最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular24. Can I bind a function to ngModel? - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited Jan 22, 2018 at 7:58 Vikas Yadav 3,3542 gold badges22 silver badges22 bronze badges asked Jan 22, 2018 at 6:58 user2031428user2031428 1,1872 gold badges9 silver badges14 bronze badges 1
  • Check if exist filter.nodes: [ngModel]="filter?.nodes?formatDate(filter.nodes[0]):null" (ngModelChange)="filter?.nodes && filter.nodes[0] = updateDate($event)" – Eliseo Commented Feb 3, 2021 at 11:20
Add a ment  | 

3 Answers 3

Reset to default 7

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
}

If you are getting data from API , then don't use dot notation. Because while loading DOM you don't have 'node' property in 'filter' variable. So try using filter['node'][0] and additionally you can also check that input field will show after loading data from API. In your case, you can try this.

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

Let me know is it working or not.

You can use a variable to update the ngmodel and change the modal in the function

<input class="form-control" placeholder="dd-mm-yyyy" name="from" [ngModel]="dateData" (change)="updateDate($event)" ngbDatepicker #d1="ngbDatepicker">

In Ts file:-

dateData:any;
 ngOnit(){
  //call the api and write below on success response block
  this.dateData = this.formatDate(this.filter.nodes[0]);
  // Sets the initial date value
 }
  updateDate($event){
  //update your date here
     let date = logictoGetDate($event);
     this.dateData=this.formatDate(date); //
  }
发布评论

评论列表(0)

  1. 暂无评论