【问题标题】:put a value in a input text inside a ng-repeat using angularjs使用 angularjs 在 ng-repeat 内的输入文本中输入一个值
【发布时间】:2015-02-25 17:23:40
【问题描述】:

HTML:

<form role="form" ng-submit="submit3(qgradecontainer)">
                    <div data-ng-repeat="qgrade in qgradecontainer">

                           <div class="form-group">
                            <label>Quiz #{{array[$index]}}</label>
                            </div>

                            <div class="form-group">

                                <label>Correct Answers</label>
                                <input type="number" min="0" max="{{array2[$index]}}" class="form-control" placeholder="Number of items: {{array2[$index]}}" ng-model="qgrade.score" id="sc{{$index}}">

                            </div>   

                    </div>
                    <div class="text-center">
                        <input type="submit" class="btn btn-success" value="Save">
                    </div>
                </form>

控制器:

gradingSheetSrvc.getNumOfitems($scope.qtbl).then(function(msg){
                    if(msg.data){
                        $scope.array2 = $.map(msg.data[0], function(value, index) {
                            return [value];
                        });
                        $scope.ind = $.map(msg.data[0], function(value, index) {
                            return [index];
                        }); 
                        //console.log($scope.array2[1]);
                        for(var i=1; i<=len; i++){
                                $scope.qgradecontainer.push({'id':$scope.array[i-1],'sid':id,'col':$scope.ind[i-1],'tbl':$scope.qtbl,'numofitems':$scope.array2[i-1]});
                                document.getElementById("sc"+i--).value = "Test";
                            }



                    }
                });

有什么方法可以使用此代码将文本放入输入文本中?因为我试图在控制器内的输入中使用document.getElementById("sc").value 输入文本,但我总是收到一条错误消息,指出Cannot set property 'value' of null

【问题讨论】:

    标签: javascript angularjs loops dom ng-repeat


    【解决方案1】:

    首先你不应该在控制器中操作 DOM,如果你想设置像这样的值;

    <input type="number" value="100">
    

    其他一切都保持不变,只需将 value 属性设置为您想要的值

    【讨论】:

    • 但我不知道如何在 qgrade.score 的 ng-model 中传递数据。我的目标是放置一个保存在我的数据库中的现有成绩,然后将其放入输入文本中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2017-11-27
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2014-09-05
    相关资源
    最近更新 更多