@ ng-select addTag,添加新标签后如何验证? [英] @ng-select addTag, how to validate after add the new tag?

查看:51
本文介绍了@ ng-select addTag,添加新标签后如何验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @ ng-select/ng-select

html文件

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

addTag 正在运行,但 addTagPromise函数在已经定义的值内部无法访问

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

ts文件

  _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);
     })
  }

打印的日志是未定义

如何访问addTagPromise函数内部已定义的值?

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

推荐答案

对不起,如果我迟到聚会,但问题是由如何处理"this"的绑定引起的.因为您只是类中已声明的方法,所以这是隐含的(对不起,我不知道该如何处理),所以最好的方法是将您的方法更改为

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 
     }

  }

以这种方式进行操作会使其绑定,因为箭头函数会自动绑定您的this.

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!

这篇关于@ ng-select addTag,添加新标签后如何验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆