我读过关于安古拉语的文章,3种儿童与家长交流的方式。如果有错误,请尽可能演示一下。
1)输出发射器
2)使用ViewChild
3)共享服务。
所以在这里,我需要理解如何将视图子对象从一个孩子传递给另一个家长。
在下面的演示中,已经在子组件中创建了一个窗体,当子组件窗体有效时,它应该反映在父组件中。在这个演示中,当组件加载到ngafterviewinit中时,钩住视图子值,它按预期工作,但是当键入某些内容时,子组件窗体是有效的,在子窗体中启用了按钮,这些更改没有反映在需要有效的父组件中,但不能按预期工作。有人能给出最好的方法吗?
父组件.html
<h1> Parent Component</h1>
<button class="btn btn-danger " disabled="{{check}}">CheckParentEnable</button>
<div class="childComponent">
<app-child-component></app-child-component>
k2
</div>
父组件.html
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ChildComponent } from './child/child.component';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
public check: boolean;
@ViewChild(ChildComponent) myname: ChildComponent;
constructor() {
}
ngAfterViewInit() {
this.check = this.myname.loginForm.valid;
}
}
child.component.html(子组件.html)
<h4>childComponentArea<h4>
<h1>Welcome to child component</h1>
<form [formGroup]="loginForm">
<input type="email" formControlName="email" placeholder="Email" >
<button class="btn btn-danger" [disabled]="loginForm.invalid">Submit</button>
</form>
子组件.ts
import { Component, EventEmitter, Input,Output, OnInit } from '@angular/core';
import { FormControl, FormGroup,Validators } from '@angular/forms';
@Component({
selector: 'app-child-component',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
loginForm:FormGroup;
name:string ='sahir';
constructor() { }
ngOnInit() {
this.createForm();
}
private createForm() {
this.loginForm = new FormGroup({
// tslint:disable-next-line
email: new FormControl('', [Validators.required])
});
}
k8
}
demo