Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Unable to run external functions inside a particular function

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?

like image 388
Rosenberg Avatar asked Aug 01 '18 20:08

Rosenberg


2 Answers

You are passing a reference to an object method, but the value of this is not being set. So you need to bind(this) to the function reference.

public addTagNowRef: (name)=>void;

constructor(public afs: AngularFirestore) {
  this.categoriesCollection = this.afs.collection('categories', ref => ref.orderBy('title', 'asc'));
  this.addTagNowRef = this.addTagNow.bind(this);
}

Then use that property in the template.

<ng-select [(ngModel)]="client.categoryId"
           class="form-control"
           [ngClass]="{'is-invalid':clientCategoryId.errors && clientCategoryId.touched}"
           #clientCategoryId="ngModel"
           name="categoryId"
           [addTag]="addTagNowRef"
           required>
  <ng-option *ngFor="let cat of cats" [value]="cat.id">{{cat.title}}</ng-option>
</ng-select>

Alternatively, you can use an arrow function to forward the call to the method.

public addTagNowRef: (name)=>void;

constructor(public afs: AngularFirestore) {
  this.categoriesCollection = this.afs.collection('categories', ref => ref.orderBy('title', 'asc'));
  this.addTagNowRef = (name) => this.addTagNow(name);
}

The point here is that this must reference the component.

like image 139
Reactgular Avatar answered Oct 02 '22 20:10

Reactgular


I solved this problem by using .bind(this) :

<ng-select [(ngModel)]="client.categoryId"
           class="form-control"
           [ngClass]="{'is-invalid':clientCategoryId.errors && clientCategoryId.touched}"
           #clientCategoryId="ngModel"
           name="categoryId"
           [addTag]="addTagNow.bind(this)"
           required>
  <ng-option *ngFor="let cat of cats" [value]="cat.id">{{cat.title}}</ng-option>
</ng-select>
like image 32
Ousama Avatar answered Oct 02 '22 19:10

Ousama