Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible pass the value of an id from a template, to function (AngulasJS2)

Tags:

angular

It is possible pass the value of an id from a template, to 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'm using it and it works,

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

but with this code does not work.

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

I'm sorry for my English

like image 280
Angel Angel Avatar asked Oct 18 '22 15:10

Angel Angel


1 Answers

When you add a template variable like #elem (on a native DOM element, not an Angular component or an element with a directive) you can use it as a reference to the element and then get the id 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"
like image 140
Günter Zöchbauer Avatar answered Oct 21 '22 04:10

Günter Zöchbauer