@ng-select addTag, how to validate after add the new tag?

元气小坏坏 提交于 2021-02-08 05:44:22

问题


i am using @ng-select/ng-select

html file

<ng-select [items]="_Customers"
           [addTag]="addTagPromise"
           [bindLabel]="'name'"
           [(ngModel)]="selectedCustomer">
</ng-select>

addTag is working but addTagPromise function inside already defined values not accessible

ts file

  _Customers: any[] = [];
  companies = ['company one', 'company Two', 'company three'];
  selectedCustomer;

  ngOnInit() {
     this.companies.forEach((c, i) => {
         this._Customers.push({ id: i, name: c });
     });
  }

  addTagPromise(name) {
    console.log(this._Customers);
     return new Promise((resolve) => {
        setTimeout(() => {
            resolve({ id: 5, name: name, valid: true });
         }, 1000);
     })
  }

printed log is undefined

how to access the already defined values inside of addTagPromise function?


回答1:


Hey sorry if i'm late to the party but the issue is caused by how the binding of "this" is handled. because you are simply a declared method in your class the this is implied (sorry I dont know exactly how that is handled) so the best method is to just change your method to

  addTagPromise: (value: any): Promise<any>;
  ngOnInit() {
     ...
     this.addTagPromise = ()=>{
       //Code the function here 
     }

  }

doing it this way will make it so that your function's this is binded because an arrow function automatically binds your this.

Again, sorry for the late reply!



来源:https://stackoverflow.com/questions/52780465/ng-select-addtag-how-to-validate-after-add-the-new-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!