最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add input fields dynamically in angularjs on button click? - Stack Overflow

programmeradmin1浏览0评论

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?

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

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

How can I acheive this using angularjs?

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

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

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

How can I acheive this using angularjs?

Share Improve this question edited Jun 11, 2015 at 14:31 Pedro del Sol 2,8419 gold badges41 silver badges52 bronze badges asked Apr 24, 2015 at 3:09 user944513user944513 12.7k51 gold badges185 silver badges346 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

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

发布评论

评论列表(0)

  1. 暂无评论