问题
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