在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)