I\'ve a component that takes function as input. I\'ve passed this function from parent.
Though the function is called, the function is not able to access the depende
You need to .bind(this)
if you pass methods around:
<custom-element [valFn]="customVal.bind(this)"></custom-element>
or
export class App {
constructor(private service: CustomService) {
}
customVal(): string {
return this.service.getVal();
}
customValFn = this.customVal.bind(this);
}
with
<custom-element [valFn]="customValFn"></custom-element>
You can pass a get/set property instead of a function in a similar way like that:
Somewhere in your view:
<input type="text" [(ngModel)]="yourprop">
In your component file:
@Component({
selector: 'myapp',
templateUrl: './myapp.component.html',
styleUrls: ['./myapp.component.scss']
})
export class App {
constructor() { }
yourprop: any;
get yourprop(): any {
return this.scheduleEndDate;
};
//set accessor including call the onchange callback
set yourprop(v: any) {
// TODO do something else
// You can do whatever you want just like you have passed a function
if (v !== this.scheduleEndDate) {
this.scheduleEndDate = v;
}
}
}
more info @ https://almerosteyn.com/2016/04/linkup-custom-control-to-ngcontrol-ngmodel