How can I use the ng-change event in angular 2? Whenever the ng-model variable is changed, a function has to be called.
[(ngModel)]=\"variable\"
ngchange=variab
component have two-way binding
()
for output[]
for inputthat means you can use ==>[value]="variable"
<== for showing data on html
and ==>(input)="setVariable($event)"
<== to update your var in ts/js.
event.target.value
FYI==>https://angular.io/docs/ts/latest/guide/user-input.html
You could use the ngModelChange
event:
[(ngModel)]="variable" (ngModelChange)="doSomething($event)"
Edit
According to your comment, I think that you should use form control with a custom validator.
Here is a sample:
@Component({
(...)
template: `
<input [(ngModel)]="variable" [ngFormControl]="ctrl"/>
`
})
export class SomeComponent {
constructor() {
this.ctrl = new Control('', (control) => {
// validate the value
});
this.ctrl.valueChanges.subscribe((value) => {
// called when the value is updated
});
}
}
See this article for more details: