ng-repeat在动态插入元素时不更新DOM

时间:2013-09-25 12:24:17

标签: json forms angularjs json-framework

我正在使用jsonForm从3个不同的json模式生成表单。提交表单时,请求另一个表单,依此类推......所有3个表单上的数据创建一个json对象,在填写完最后一个表单后将其发回并存储。

表单按预期工作,问题是更新列表使用ng-repeat

HTML:

<ul>
    <li ng-repeat="names in newName">{{names}}</li>
<ul>

默认名称数组:

$scope.newName= ["test1","test2"];

初始对象

var set1 = {
     "setName": "",
     "setAttributes": [],
    "setQuestions": []

};

角:

        $scope.createNewSet = function () {
            $http.get('/json/schema1.json').success(function(data) {
                $('#form1').jsonForm({ // DISPLAY FIRST FORM
                 schema: data,
                 "form": [
                   "*",
                   {
                     "type": "submit",
                     "title": "Proceed to step 2"
                   }
                 ],
                 onSubmit: function (errors, values) { 
                    var sName = values.setName;
                    set1.setName = sName;
                    $http.get('/json/schema2.json').success(function(data) {
                        $('#form2').jsonForm({ //DISPLAY SECOND FORM
                        schema: data,
                        "form": [
                          "*",
                          {
                            "type": "submit",
                            "title": "Proceed to step 3"
                          }
                        ], 
                        onSubmit: function (errors, values) { // DISPLAY THIRD FORM
                            set1.setAttributes = values.attributes;
                            $http.get('/json/schema3.json').success(function(data) {
                            $('#form3').jsonForm({
                               schema: data,
                               "form": [
                                  "*",
                                 {
                                  "type": "submit",
                                  "title": "Proceed to step 4"
                                  }
                                ],
                                onSubmit: function (errors, values) { // SAVE COMPLETE SET
                                  set1.setQuestions = values.questions;
                                  $scope.newName.push(set1.setName);  //UPDATE li
                                  $scope.newName.push('blah')
                                    console.log($scope.newName);
                                },
                              });
                            });
                          },   

浏览器控制台显示带有["test1","test2","Simo","blah"]Simo的更新的newName数组Blah,但HTML不会显示它们。是否有一些阻止角度更新DOM的东西?

谢谢..

1 个答案:

答案 0 :(得分:0)

尝试重新应用范围:

$scope.$apply(function(){
    $scope.newName.push(set1.setName);  //UPDATE li
    $scope.newName.push('blah')
});