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



我有一个输入字段,一旦填充了一个数字,就会填充额外的输入字段。我不知道如何在那些生成的输入字段中插入值。感谢您的帮助,哦,我确实尝试过各种方法,甚至使用angular元素,或者仅使用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>

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

因此,将ng-model="inputs[i-1]"添加到您的文本字段中

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

然后在您的getValue函数中:return $scope.inputs[id-1];

如果您将getTimes函数设置为0,则不需要-1

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

最新更新