You can pass the id value from the template so that the function (AngulasJS2)

You can pass the id value from the template to the function (AngulasJS2).

template: ` <div class="container" *ngFor="#mov of movs"> ..// <button class="btn btn-primary" type="button" ..// (click)="test(id)" [id]="mov" > ..// ..// test(id: string) { //test var logo1: HTMLElement = document.getElementById(id); ..// } 

right now i use it and it works,

  (click)="test(''+mov)" [id]="mov" 

but does not work with this code.

 (click)="test(id)" [id]="mov" 

Sorry for my english

+5
source share
2 answers

When you add a template variable, such as #elem (to your own DOM element, and not to an Angular component or an element with a directive), you can use it as a reference to the element, and then get the identifier from the element ( elem.id ).

 <div class="container" *ngFor="let mov of movs" > ..// <button class="btn btn-primary" type="button" #elem ..// (click)="test(elem.id)" [id]="mov" 
+2
source

You can simply do this:

 <div class="container" *ngFor="let mov of movs" > ..// <button class="btn btn-primary" type="button" ..// (click)="test(mov)" [id]="mov"> 

plunker link: https://plnkr.co/edit/iFRJI8perdwmWpZoBcvW?p=preview

0
source

Source: https://habr.com/ru/post/1242786/


All Articles