我想在 Angular 5 中实现一个带有控制值访问器的功能。这就像从父级访问子自定义组件中的多个表单控件。请让我知道我是否可以通过其他方式实现这一目标。模板驱动形式是强制性的。
如果有任何其他可以通用创建具有双向数据绑定的自定义控件,请告诉我。如果答案在 Plunker 或 StackBlitz 中,那就太好了。
这是我的:https ://stackblitz.com/edit/angular-qzioet
父组件:-
export class AppComponent implements OnInit {
public countryList: any = [];
public option: any =[ ];
public personal = {
identity: {
name: {firstname: null, lastname: null },
age: null,
sex: null
}
}
@ViewChild('personalForm') form: any;
constructor() {
}
父 html:-
<app-input name ='name'
[(ngModel)]="personal.identity.name" [form]="personalForm"
ngDefaultControl></app-input>
子组件:-
import {Component, Input, forwardRef} from '@angular/core'
import {
FormControl,
FormGroup,
ControlValueAccessor,
NG_VALUE_ACCESSOR,
NG_VALIDATORS,
Validator
} from '@angular/forms';
@Component({
selector: 'app-input',
templateUrl: "./input-child.html",
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Child),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => Child),
multi: true,
}
]
})
export class Child implements ControlValueAccessor, Validator {
@Input() form: any;
@Input() name: any;
childControl = new FormGroup({ firstname: new FormControl() ,
lastname: new FormControl() });
fn: (value: any) => void;
constructor() {
console.log(this.childControl);
}
writeValue(value: any) {
if (value) {
this.childControl.setValue(value);
}
}
registerOnChange(fn: (value: any) => void) {
this.fn = fn;
}
registerOnTouched() {}
validate(c: FormControl) {
return this.childControl.errors;
};
}
子html:-
`<h1>Child</h1>
<div>
<input [formControl]="firstname" (input)="fn($event.target.value)"
required>
<input [formControl]="lastname" name="lastname"
(input)="fn($event.target.value)" required>
</div>`