I'm using ng-select for angular 6.
This is the HTML side of it:
<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>
And this is the typescript:
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);
}
This is the error:
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)
If I run the code outside of AddTagNow
function it works perfectly fine.
How can I execute that code?