Angular 角度8:当用户点击子组件文本框时,让父组件检测
我有一个父组件和一个带有Formbuilder和ZipCode文本框的子组件 我如何做到这一点,当用户从子组件文本框中单击时,父组件将知道? 需要在用户点击文本框后运行业务逻辑。当前,textbox基于材质角包装 子组件有Formbuilder和许多文本框,示例如下Angular 角度8:当用户点击子组件文本框时,让父组件检测,angular,typescript,angular8,Angular,Typescript,Angular8,我有一个父组件和一个带有Formbuilder和ZipCode文本框的子组件 我如何做到这一点,当用户从子组件文本框中单击时,父组件将知道? 需要在用户点击文本框后运行业务逻辑。当前,textbox基于材质角包装 子组件有Formbuilder和许多文本框,示例如下 this.editAddressForm = this.formBuilder.group({ 'city': [null,[Validators.maxLength(50)]], 'zipCode': [null,[V
this.editAddressForm = this.formBuilder.group({
'city': [null,[Validators.maxLength(50)]],
'zipCode': [null,[Validators.maxLength(10)]],
<app-input-textbox class = "zipcodeclass" div="zipcode" formControlName = "zipCode">
this.editAddressForm=this.formBuilder.group({
“城市”:[null,[Validators.maxLength(50)],
“zipCode”:[null,[Validators.maxLength(10)],
您需要在blur
事件上向父级发出事件
在HTML模板中,添加(blur)=“onBlur()”
,并在每个输入上添加一个模板引用(比如#templaterreference1
,#templaterreference2
和#templaterreference3
)
然后在子元素中:
@Output() noInputFocused: = new EventEmitter();
@ViewChild('templateReference1') templateReference1: ElementRef;
@ViewChild('templateReference2') templateReference2: ElementRef;
@ViewChild('templateReference3') templateReference3: ElementRef;
inputs;
constructor(@Inject(DOCUMENT) private document: Document) {}
ngOnInit() {
this.inputs = [templateReference1, templateReference2, templateReference3];
}
onBlur() {
if(this.inputs.map(x => x.nativeElement).indexOf(this.document.activeElement) < 0) {
this.noInputFocused.next();
}
}
@Output()noInputFocused:=neweventEmitter();
@ViewChild('templateReference1')templateReference1:ElementRef;
@ViewChild('templateReference2')templateReference2:ElementRef;
@ViewChild('templateReference3')templateReference3:ElementRef;
投入;
构造函数(@Inject(DOCUMENT)private-DOCUMENT:DOCUMENT){}
恩戈尼尼特(){
this.inputs=[templateReference1,templateReference2,templateReference3];
}
onBlur(){
if(this.inputs.map(x=>x.nativeElement).indexOf(this.document.activeElement)<0){
this.noInputFocused.next();
}
}
已接受答案,问题写错了,请回答另一个问题,谢谢