So I'm trying to make a directive that can manipulate a FormControl.
It seems that if I use the long syntax for declaring form controls in the template instead, I can pass the control to a directive to do stuff with it as a direct @Input() bind; i.e.: With the following template:
<form [formGroup]="myForm">
<input type="text" id="myText" [formControl]="myForm.controls['myText']" my-directive>
</form>
And the following component logic:
@Component({
// Properties go here.
})
class MyComponent {
myForm: FormGroup;
constructor(fb: FormBuilder) {
// Constructor logic...
}
ngOnInit() {
this.myForm = this.fb.group({
"myText": [""]
});
}
}
The directive would look like:
@Directive({
selector: "[my-directive]"
})
class MyDirective {
Input() formControl: FormControl;
}
But if I were using the formControlName syntax in the template instead:
<form [formGroup]="myForm">
<input type="text" id="myText" formControlName="myText" my-directive>
</form>
How would I reference the (implicitly?) made FormControl in the directive?
formControlName="someName"
or[formControl]="myForm.controls['someName']"
near as I can tell it doesn't make a difference. If you post your directive and why its not working for you then perhaps you can get an answer. – silentsod