angular - Angular2 How to get reference of HTML elements generated dynamically -


i have inputs generated dynamically:

  <div *ngfor="let cell of column; let = index;">           <!-- material design input-->           <md-input id="my-input-{{i}}">           </md-input>   </div> 

please notice id=my-input-{{i}} reference dom element based on dynamic id. input can 3, 6 or more inputs need access id dynamically , hold it.

use elementref class @angular/core parent element , create htmlelement dynamic elements class name.

component:

declare var $: any; //intentional use of jquery-not recommended though  @component({   selector: 'my-app',   template: `     <input type='button' (click)='adddiv()' value='add new div' />      <input type='button' (click)='selectdiv()' value='select' />   ` }) export class app {   constructor(private elref: elementref) {   }    adddiv() {     /*intentional use of jquery appending new div      give class dynamic elements*/      $('my-app').append("<div class='foo'>hello i'm dynamic!</div>");   }    selectdiv() {     //create new htmlelement nativeelement     var helement: htmlelement = this.elref.nativeelement;      //now can elements class name     var alldivs = helement.getelementsbyclassname('foo');      //do selected elements     console.log(alldivs);   } } 

working plunker

edit: used jquery quick demo purpose here. otherwise, you should not use jquery angular.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -