Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度8:当用户点击子组件文本框时,让父组件检测_Angular_Typescript_Angular8 - Fatal编程技术网

Angular 角度8:当用户点击子组件文本框时,让父组件检测

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

我有一个父组件和一个带有Formbuilder和ZipCode文本框的子组件

我如何做到这一点,当用户从子组件文本框中单击时,父组件将知道? 需要在用户点击文本框后运行业务逻辑。当前,textbox基于材质角包装

子组件有Formbuilder和许多文本框,示例如下

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();
}
}

已接受答案,问题写错了,请回答另一个问题,谢谢