[英]Angular click event not fired when custom input is focused
I created simple custom input eu-input
element using 'ControlValueAccessor'.我使用“ControlValueAccessor”创建了简单的自定义输入
eu-input
元素。 Now I have something like that:现在我有类似的东西:
<eu-input></eu-input>
<div *ngFor="let item of items" (click)="choose(item)">
<span>{{item.name}}</span>
</div>
But there is problem I can't figure out.但是有一个问题我想不通。 When
eu-input
is focused, click event doesn't get fired, it's fired only after eu-input
is blured (on second attempt).当
eu-input
聚焦时,click 事件不会被触发,它仅在eu-input
模糊后触发(第二次尝试)。 so what could be a problem?那么可能是什么问题?
this is html:这是 html:
<input [(ngModel)]="value"/>local: {{val}}
and this is ts file:这是 ts 文件:
import { Component, OnInit, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'eu-input',
templateUrl: './eu-input.component.html',
styleUrls: ['./eu-input.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EuInputComponent),
multi: true,
},
],
})
export class EuInputComponent implements OnInit, ControlValueAccessor {
constructor() {}
ngOnInit(): void {}
onChange: any = () => {};
onTouch: any = () => {};
val = '';
set value(val) {
if (val !== undefined && this.val !== val) {
this.val = val;
this.onChange(val);
this.onTouch(val);
}
}
writeValue(value: any) {
this.value = value;
}
registerOnChange(fn: any) {
this.onChange = fn;
}
registerOnTouched(fn: any) {
this.onTouch = fn;
}
}
choose() method is just console.log('choose clicked') choose() 方法就是 console.log('choose clicked')
Change eu-input component html to将 eu 输入组件 html 更改为
<input [(ngModel)]="value" (focus)="emitFocused(true)" (blur)="emitFocused(false)"/>local: {{val}}
In eu-input TypeScript在欧盟输入 TypeScript
@Output() focus = new EventEmitter();
public emitFocused(value) {
this.focus.emit(value);
}
Can be used like可以像这样使用
<eu-input (focus)="myMethod($event)"></eu-input>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.