【问题标题】:how to add input fields dynamically in angularjs on button click?如何在按钮单击时在 angularjs 中动态添加输入字段?
【发布时间】:2015-04-24 03:09:56
【问题描述】:

我正在尝试制作一个简单的示例,其中每次用户单击按钮时都会出现输入字段和按钮字段。

当单击与输入字段一起出现的新按钮时,如何获取输入字段的文本?

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

})

我不知道该怎么做;在 jQuery 中我们可以使用 append 函数,就像这样

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

如何使用 angularjs 实现这一点?

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    你可以做的是使用一个数组来表示输入,然后循环遍历数组并渲染它

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

    然后

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

    演示:CodePen

    【讨论】:

    • 感谢您的回答,但如何获取从动态生成的按钮动态生成的输入字段的值
    • 你想要怎样的值?
    • 从动态按钮点击
    • 角度如何区分每个 item.value 与其余部分?我不明白这种关心,请多解释一下?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多