Angular2/4. Can I bind a function to ngModel?

10,319

You may use (ngModelChange)="yourMethod($event)" to bind ngModel to a method

<input label="Choose a year" (ngModelChange)="selectYearGraph($event)" [(ngModel)]="chosenYearDate" ></input>

then in your ts file

yourMethod($event){
//do your stuff
}
Share:
10,319
user2031428
Author by

user2031428

Updated on June 21, 2022

Comments

  • user2031428
    user2031428 almost 2 years

    I am using ngbDatePicker from the ng-bootstrap. I am trying to set a default date which is returned from an API. This doesn't seem to work as either i get errors or the browser freezes. The code snippet looks like this

    <input class="form-control" placeholder="dd-mm-yyyy" name="from" [ngModel]="formatDate(filter.nodes[0])" (ngModelChange)="filter.nodes[0] = updateDate($event)" ngbDatepicker #d1="ngbDatepicker">

    filter.nodes[0] is the date value that is returned from the API as a string. I need to format this into a JS Object for it to bind to the input.