Filipe Nóbrega
Filipe Nóbrega

Reputation: 667

(click) with a ngFor Variable

I'm trying to execute a function from a variable the following way:

<ng-container *ngFor="let c of content">
            <div class="content-box" [style.background-color]="c.color" [matTooltip]="c.name" (click)="c.select"> 
            .....

My content:

this.content = [
      {name: '...',data: 13, code: 23, color: 'gray', select: 'testFunction()'},
      ....
      ];

Is this possible?

Update 1 My test.component:

ngOnInit() {
    this.user$ = this.store.pipe(select(currentUser));

    this.content = [
      {name: '...',data: 13, code: 23, color: 'gray', select: 'testFunction'},
      ....
    ];
  }

  testFunction() {
    console.log("Test.");

  }

Upvotes: 2

Views: 426

Answers (2)

yurzui
yurzui

Reputation: 214047

You can try accessing method through square brackets:

html

(click)="this[c.select]()"

ts

@Component({...})
class SomeComponent {
  content = [
   {name: '...',..., select: 'testFunction'},
  ];

  testFunction() {
    console.log('test clicked');
  }
}

Upvotes: 1

Piercy
Piercy

Reputation: 809

select: 'testFunction()' should be just select: testFunction. You don't want the brackets or the single quotes. Specifying it how you have makes it a string, not a function.

As well as that i think you need to do c.select() on the click property.

Upvotes: 1

Related Questions