我将 ng-select 用于角度 6。
这是它的 HTML 方面:
<ng-select [(ngModel)]="client.categoryId"
class="form-control"
[ngClass]="{'is-invalid':clientCategoryId.errors && clientCategoryId.touched}"
#clientCategoryId="ngModel"
name="categoryId"
[addTag]="addTagNow"
required>
<ng-option *ngFor="let cat of cats" [value]="cat.id">{{cat.title}}</ng-option>
</ng-select>
这是打字稿:
nCategory: Category = {
title: ''
};
constructor(public afs: AngularFirestore) {
this.categoriesCollection = this.afs.collection('categories', ref => ref.orderBy('title', 'asc'));
}
addTagNow(name) {
this.nCategory.title = name;
this.categoriesCollection.add(this.nCategory);
}
这是错误:
NgSelectComponent.html:91 ERROR TypeError: Cannot set property 'title' of undefined
at NgSelectComponent.push../src/app/components/edit-client/edit-client.component.ts.EditClientComponent.addTagNow [as addTag] (edit-client.component.ts:169)
如果我在AddTagNow
函数之外运行代码,它工作得很好。
我该如何执行该代码?