Q1. Is it possible to have one Control ie:
ValidNumber = new Control(\'\', CustomValidators.number({min:1, max:10}))
to
You can also generate the controls then there is no problem.
@Component({
...
template: `
...
<input *ngFor="let c in controls" [ngFormControl]="c" name="c.name" type="number"/>
...
`
})
class MyComponent {
// initialization with `['a', 'b', 'c']` just for demo purposes
// these values probably come from outside - hence @Input()
@Input() controlNames:string[] = ['a', 'b', 'c'];
controls: Control[];
ngOnInit() {
this.controlNames.forEach(
v => this.controls.push(
new Control('', CustomValidators.number{min:1, max:10})
)
);
}
}
(code not tested)
controls
needs to be updated when controlNames
changes. ngOnInit()
runs only once.