小编Pra*_*han的帖子

在angular2中,输入键触发页面上的任何点击事件?

removeSelectedCountry()单击span元素时应该调用下面的代码,并且span当div上有keydown事件时应该调用它.

@Component({
    selector: "wng-country-picker",
    template: `
    <ul class="CountryPicker-selected" *ngIf="selectedCountries.length > 0">
    <li *ngFor="let country of selectedCountries">
        <span class="Pill Pill--primary" (click)="removeSelectedCountry(country)">
        {{ country.name }}
        </span>
    </li>
    </ul> 
    <div (keydown)="handleKeyDown($event)" class="CountryPicker-input"></div>
`,
providers: [CUSTOM_VALUE_ACCESSOR]
})
Run Code Online (Sandbox Code Playgroud)

但是handleKeyDown($event)每次按下键都被调用.

为了使代码工作,我不得不将click事件更改为keydownevent.它现在工作正常.

任何人都可以解释为什么输入键会触发点击事件?

@Component({
    selector: "wng-country-picker",
    template: `
    <ul class="CountryPicker-selected" *ngIf="selectedCountries.length > 0">
    <li *ngFor="let country of selectedCountries">
        <span class="Pill Pill--primary" (mousedown)="removeSelectedCountry(country)">
        {{ country.name }}
        </span>
    </li>
    </ul> 
    <div (keydown)="handleKeyDown($event)" class="CountryPicker-input"></div>
`,
providers: [CUSTOM_VALUE_ACCESSOR]
})
Run Code Online (Sandbox Code Playgroud)

添加类snipppet

export class CountryPickerComponent …
Run Code Online (Sandbox Code Playgroud)

dom-events angular

55
推荐指数
5
解决办法
9万
查看次数

标签 统计

angular ×1

dom-events ×1