2

我有一个输入字段,一旦填充了一个数字,就会填充该额外输入字段的数量。我不知道如何在这些生成的输入字段中插入值。任何帮助表示赞赏,哦,我确实尝试了各种事情,甚至使用 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>

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

1 回答 1

6

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

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

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

如果你让你的 getTimes 函数基于 0 你就不需要-1s

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

于 2013-07-09T13:39:43.647 回答