使用Angular中的ng-repeat获取/设置动态生成的输入字段的值

Nik*_*ola 2 angularjs angularjs-ng-repeat

我有一个输入字段,一旦填充了数字,就会填充其他输入字段的数量.我无法弄清楚如何在这些生成的输入字段中插入值.任何帮助是值得赞赏的,哦,我确实尝试过各种各样的东西,甚至只使用angular.element,或者只使用jquery,但是失败了,所以对于如何做到这一点的任何解释都是受欢迎的.

这是我的jsfiddle,代码c/p-ed在这里:

<div ng-controller="MyCtrl">
    <input type="text" ng-model="cntInputs" placeholder="#Inputs"><br/>

    <input ng-repeat="i in getTimes()" type="text" placeholder="{{i}}" id="input_{{i}}">

    <ul>
        <li ng-repeat="j in getTimes()">
            Inputed value is: {{getValue(j)}}
        </li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

var myApp = angular.module('myApp', []);


function MyCtrl($scope) {
    $scope.cntInputs = 0;

    $scope.getTimes=function(){
        var a = new Array();

        for(var i=1; i <= $scope.cntInputs; i++)
            a.push(i);

        return a;       
    };

    $scope.getValue = function(id){
        //here get the value of that inserted in the element with the id of "input_" + id
        return angular.element("input_" + id);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*eeh 6

所以添加ng-model="inputs[i-1]"到您的文本字段

然后在你的控制器中添加 $scope.inputs= [];

然后在你的getValue功能: return $scope.inputs[id-1];

如果你使你的getTimes函数为0,你将不需要-1s

更新了你的小提琴:http://jsfiddle.net/7MhLd/60/