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

Pra*_*han 55 dom-events angular

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 {

private selectedCountries: CountrySummary[] = new Array();

private removeSelectedCountry(country: CountrySummary){
    // check if the country exists and remove from selectedCountries
    if (this.selectedCountries.filter(ctry => ctry.code === country.code).length > 0)
    {
        var index = this.selectedCountries.indexOf(country);
        this.selectedCountries.splice(index, 1);
        this.selectedCountryCodes.splice(index, 1);
    }
}

private handleKeyDown(event: any)
{
    if (event.keyCode == 13)
    {
       // action
    }
    else if (event.keyCode == 40)
    {
        // action
    }  
    else if (event.keyCode == 38)
    {
        // action
    }    
}
Run Code Online (Sandbox Code Playgroud)

Ang*_*hub 134

对于ENTER键,为什么不使用(keyup.enter):

@Component({
  selector: 'key-up3',
  template: `
    <input #box (keyup.enter)="values=box.value">
    <p>{{values}}</p>
  `
})
export class KeyUpComponent_v3 {
  values = '';
}
Run Code Online (Sandbox Code Playgroud)


小智 34

使用(keyup.enter).

Angular可以为我们过滤关键事件.Angular具有键盘事件的特殊语法.我们可以通过绑定到Angular的keyup.enter伪事件来监听Enter键.


And*_*ban 34

这是正确的解决方案!由于该按钮没有定义的属性类型,因此angular可能会尝试将keyup事件作为提交请求发出,并触发按钮上的click事件.

<button type="button" ...></button>
Run Code Online (Sandbox Code Playgroud)

非常感谢DeborahK!

Angular2 - Enter Key执行表单上的第一个(单击)功能


小智 12

@Component({
  selector: 'key-up3',
  template: `
    <input #box (keyup.enter)="doSomething($event)">
    <p>{{values}}</p>
  `
})
export class KeyUpComponent_v3 {
  doSomething(e) {
    alert(e);
  }
}
Run Code Online (Sandbox Code Playgroud)

这适合我!


小智 6

对于角度6,有一种新的处理方法。在您的输入标签上添加

(keyup.enter)="keyUpFunction($event)"
Run Code Online (Sandbox Code Playgroud)

keyUpFunction($event)你的职能在哪里。