简体   繁体   中英

Issue on how to add jQuery into angular 2 project

Hi I want to add some jquery code into my angular 2 projet to add input text field dynamically, its work, but the removeTextField doesnt execute when I click on it heres the code:

addTextField(){
var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + this.counter);

newTextBoxDiv.after().html('<label>Textbox #'+ this.counter + ' : </label>' +
      '<input type="text" name="textbox' + this.counter +
      '" id="textbox' + this.counter + '" value="" >'+'<span (click)="removeTextField(this.counter)" class="glyphicon glyphicon-remove pull-right" style="z-index:33;cursor: pointer">  </span>');
newTextBoxDiv.appendTo("#TextBoxesGroup");
this.counter++;

}

 removeTextField(currentTextField){
  alert(currentTextField);
 }

any suggestions, please!!!

Try like this

For example if your input text field is like

<input type="text" class="my-input-field">

In your controller

$(document).click("on", ".my-input-field", ()=>{
   //capture the currentTextField using jquery
   this.removeTextField(currentTextField);
})

   removeTextField(currentTextField){

}

Example Its a datatable implemented in angular 2.. The update and delete buttons are rendered on the fly.

HTML

<table class="datatable table table-striped table-bordered" (click)="onTableClick($event)"></table>

Controller

onTableClick(event){

   if(event.target.className.indexOf('update-row')!= -1){ //will get the class name and check if matches update button class
     this.updateRow($(event.target).attr("data-id"))
   }
   if(event.target.className.indexOf('delete-row')!= -1){ //will get the class name and check if matches delete button class
     this.deleteRow($(event.target).attr("data-id"))
   }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM