关于如何将jQuery添加到angular 2项目中的问题

时间:2017-09-06 15:10:57

标签: javascript jquery angular

您好我想在我的angular 2 projet中添加一些jquery代码来动态添加输入文本字段,它的工作,但是当我点击它时,removeTextField不执行代码:

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);
 }

任何建议,请!!!

1 个答案:

答案 0 :(得分:0)

试试这个

例如,如果输入文本字段类似于

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

在您的控制器中

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

   removeTextField(currentTextField){

}

示例 它是一个以角度2实现的数据表。更新和删除按钮是即时渲染的。

<强> HTML

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

<强>控制器

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"))
   }
  }