javascript - 有没有办法将输入绑定(bind)到值并使用不同的 ng-model 进行跟踪?

标签 javascript angularjs angularjs-ng-model

这是我的问题:

  • I'm using ng-repeat to create input fields from an array. [a, b, c,...]
  • I have a button that lets you add more blank input fields.
  • After clicking the save button I want to be able to create a new array from the input fields.

所以我正在考虑使用一个迭代的 ng-model (item-1, item-2, item-3,...) 来跟踪每个输入,然后在我点了保存。但这需要我将每个输入与一个不是来自 [a, b, c,....] 的新变量绑定(bind)。

所以我要问的是如何使用 AngularJS 执行此伪代码。

<input ng-repeat="letter in letters" 
  set-value="letter.value" 
  bind-to-model="someNewVariable">

最佳答案

看这个例子:

HTML :

<div class="addcontend">
      <div ng-repeat="item in inputs track by $index">
        <input ng-model="inputs[$index]" ng-value="item" />
        <button ng-click='getVal(item)'>get input value</button>
      </div>
  </div>
  <button ng-click="addfield()">Add field</button>
  <button ng-click="log()">log</button>

Controller :

 app.controller('cntr',function($scope){
    $scope.inputs = ['a', 'b'];

    $scope.addfield = function(){
       $scope.inputs.push('')
    }

    $scope.getVal = function(v){
       alert(v);
    }

    $scope.log = function() {
      console.log($scope.inputs)
    }
 })

关于javascript - 有没有办法将输入绑定(bind)到值并使用不同的 ng-model 进行跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45581525/

相关文章:

javascript - 使用 AngularJS 中的指令将对象存储在 ngModel 中

javascript - 有没有办法使用数组来简化代码?

javascript - 如何从ajax获取准确的错误消息

javascript - 时刻: Getting error when formatting date using user language

javascript - 即使命名约定正确,AngularJS 指令也未应用

angularjs - ng-checked 和 ng-change 单选按钮不能一起工作 - angularjs

javascript - 如何使用javascript动态添加videojs播放器?

javascript - Angular 元素绑定(bind)到相同的函数。避免对每个摘要进行多次调用

javascript - 等待编程滚动 Selenium /Protractor

javascript - 同步两个具有不同模型的文本框