Is it possible to use a function as a value for ngModel
in Angular? I need to be able to set the value for my input at a later point in time, so I want to check if the function exists before updating the model. However, the following code snippet is not functioning as expected:
@Component({
selector: 'string-editor',
template: `
<dl>
<dt>{{propertyName}}</dt>
<dd>
<input
type="text"
[(ngModel)]="getValue()" />
</dd>
</dl>`,
})
export class StringEditor {
@Input() public propertyName: string;
@Input() public entity: any;
getValue() {
return this.entity ? this.entity[this.propertyName] : ''
}
};