In my app I introduced a mapper directive responsible for populating form values from a class. Intended to be used as in:
<form [formGroup]="form" [myItemMapper]="item$ | async">
Not intended to be on any other element, such as form array or form control.
To achieve this intention, I restricted the selector to be [formGroup][myItemMapper], which works, ie. if you place this directive on any other element, you get a big Webpack compilation overlay with the error saying
`NG8002: Can't bind to 'myItemMapper' since it isn't a known property of '<whatever>'.
Goal reached.
But I was wondering why the following code didn't work:
constructor(
@Self() fg: FormGroupDirective,
) {
if(!fg){
console.error("'myItemMapper' can only be used on a FormGroup directive.")
}
}
- instead of a nice informative
console.error, I got a nasty "No provider for formGroupDirective found" runtime error with no explanation. For a consumer it is a misleading experience as if they didn't importReactiveFormsModuleat all.
Tried @Host() too - it basically didn't care which element the directive was on, it was able to find formGroup also when it was placed on a formControlName- which is also something I don't understand.
So - is the first option the best I can get for my directive's consumer?