Dynamic template variable inside event binding

Eaden

I have an angular component with this kind of template variable name

#name_{{i}}

inside a *ngFor, so that any component generated has a different variable name. I need to pass the entire variable (not as a string, as a reference to the template variable) to a function in a button event binding inside the same *ngfor, like this

(click)="myFunction(name_{{i}})"

Now, I cannot pass it like this

(click)="myFunction('name_'+i)"

because myFunction doesn't want a string as variable, but the template variable reference.

Passing the interpolation inside the event binding causes me this error:

Parser Error: Got interpolation ({{}}) where expression was expected

How can I pass the template variable to the function without using interpolation, as the variable is dynamic?

Pastabolo

You don't need to do so.

You don't have to generate a name for each instance or even use @viewChildren.

You can use the same reference name for each element generated by ngFor.

In fact, the reference call is automatically attached to the reference variable it is generated with inside the *ngFor loop directive (it's like a scope inside the template passed to the ngFor directive)

You can write it like this :

<div #item *ngFor="let item of list" (click)="myFunction(item)"></div>

Hope it helps :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django dynamic template variable inside if clause

Binding an Action to an Checkbox Checked event inside a MenuItem template

Binding a variable inside a service to html template in an Angular App

Dynamic event binding not firing

How to use of dynamic local template variable inside [ngClass] and ngIf

EventSource dynamic binding variable

Dynamic event template in FullCalendar

What is the advantage of using ngModel over input/event binding, template variable binding and using banana syntax - approaches for 2-way binding

Dynamic binding in Knockout click event

Event binding .on() not working, on dynamic buttons

Change binding of element inside template

Binding click event inside template with plain javascript as value in angular2

Angular 2 event binding use interpolation doesn't work inside template

Event binding - Capture variable in a closure

Ruby Variable Binding Inside Loops

Dynamic array inside of Class template

Event Callback Inside Template Literal

Jquery Mobile dynamic dialog with listview event not binding

Tkinter dynamic event binding for multiple tags

Dynamic binding of typeahead select event (jQuery)

Two way data binding inside a template

Binding background property inside template of LongListSelector

use 'id name ends with' inside on() for dynamic binding

Dynamic if inside a foreach binding to do a "group by" in the view

No exception being thrown inside SpreadJS event binding

Dynamic URL with variable django template

Rendering dynamic variable in a django template

Add dynamic variable to onclick event

dynamic image variable with onclick event