简体   繁体   English

如何在单击按钮时在 angularjs 中动态添加输入字段?

[英]how to add input fields dynamically in angularjs on button click?

I am trying to make a simple example in which an input field and a button field each time a user clicks on button.我正在尝试制作一个简单的示例,其中每次用户单击按钮时都会有一个输入字段和一个按钮字段。

How can I get the text of the input field when the new button, which is present along with input field, is clicked?单击与输入字段一起出现的新按钮时,如何获取输入字段的文本?

http://codepen.io/anon/pen/yNLGzx http://codepen.io/anon/pen/yNLGzx

var app = angular.module('ionicApp',['ionic']);
app.controller('cntr',function($scope){
     $scope.addfield=function(){
       alert("how to add input field dyanmically")
     }

})

I don't know how to do this;我不知道该怎么做; in jQuery we can use the append function, like so在 jQuery 中,我们可以使用 append 函数,就像这样

$('.addcontend').append('<input \> <button>get input value</button>')

How can I acheive this using angularjs?如何使用 angularjs 实现这一目标?

What you can do is to use an array to represent the inputs then loop through the array and render it like你可以做的是使用一个数组来表示输入然后遍历数组并渲染它

<div class="addcontend">
  <div ng-repeat="item in inputs">
    <input ng-model="item.value"/> <button ng-click='addfield()'>get input value</button>
  </div>
</div>

then然后

  $scope.inputs = [];
  $scope.addfield=function(){
    $scope.inputs.push({})
  }

Demo: CodePen演示: CodePen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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