I have a directive that seems to have broken when I upgraded to Angular 9 and build with Ivy turned on.
I made a simplified version of the directive to illustrate what the problem is:
import { Directive, Input, forwardRef, OnInit } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
@Directive({
selector: '[my-directive]',
providers: [MyDirective,
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyDirective),
multi: true,
}]
})
export class MyDirective implements OnInit, ControlValueAccessor {
@Input("my-directive") MyDirective: any;
test: any;
test2: any = { id: 2 }
ngOnInit() {
this.test = { id: 1 }
}
private propagateChange = (_: number) => { };
public writeValue(obj: number | string) {
console.log(this.test); // <- This is undefined when Ivy is on and { id: 1 } with Ivy off
console.log(this.test2); // <- This works
}
public registerOnChange(fn: any) {
this.propagateChange = fn;
}
public registerOnTouched() { }
}
and the HTML
<input type="text" [my-directive]='{ "param1": "false" }' [(ngModel)]="tempVal" />
It looks like my directive is being created twice. Once for the [my-directive]
and once for the [(ngModel)]
Any ideas what changed and how to fix this?