I want to implement a functionality with control value accessor in Angular 5. It's like to access multiple form control in child custom component from a parent. Please let me know if I can achieve this in another way. Template driven form compulsory.
If there is any other any to generic create a custom control with two-way data binding, please let me know. It would great if answers are in Plunker or StackBlitz.
Here is mine: https://stackblitz.com/edit/angular-qzioet
Parent component :-
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() {
}
Parent html:-
<app-input name ='name'
[(ngModel)]="personal.identity.name" [form]="personalForm"
ngDefaultControl></app-input>
Child component :-
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;
};
}
Child 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>`