ng-repeat不刷新DOM

时间:2016-02-09 01:16:23

标签: javascript angularjs

我正在使用ng-repeat来构建数据表,但是在获取新的json数据后DOM不会刷新。如下图所示,单击文件夹时,我可以获得正确的Json数据,但只是不在视图中更新。当我使用$scope.$apply()时,得到错误“摘要已在进行中”这是我的代码的一部分

HTML:

<tr ng-repeat="x in ::applicationimages" ng-click="rowSelect($index)" ng-class="{'selected': selectedRows.indexOf($index) != -1}">
     <td layout="row" layout-align="start center">
        <span>
        {{x.type}}
        </span>
        <a ng-class="{'folder': x.type == 'folder', 'image': x.type == 'image'}" ng-click="getInfo($index); $event.stopPropagation()">
        </a>
      </td>
      <td>
      {{x.name}}
      </td>
 </tr>

控制器:

        /*initial json data*/

       $http({
            method: 'GET',
            url: '/services/images/'
        }).then(function(response) {

            $scope.applicationimages = response.data.payload.list;

        },function(response) {

            console.log('error');
        })


     /*get new json when clicking folder*/

    $scope.getInfo = function(index) {

    if($scope.applicationimages[index].type == 'folder') {
        $http({
            method: 'GET',
            url: '/services/images/' + $scope.applicationimages[index].name
        }).then(function(response) {

          $scope.$apply(function(){
                $scope.applicationimages = response.data.payload.list;
                })

        }, function(response) {

            console.log('error');
        })                           
    } 
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您在ng-repeat中使用了一次绑定。因此,即使applicationimages发生更改,也不会在模板中重新呈现。像这样删除::

<tr ng-repeat="x in applicationimages" ng-click="rowSelect($index)" ng-class="{'selected': selectedRows.indexOf($index) != -1}">
     <td layout="row" layout-align="start center">
        <span>
        {{x.type}}
        </span>
        <a ng-class="{'folder': x.type == 'folder', 'image': x.type == 'image'}" ng-click="getInfo($index); $event.stopPropagation()">
        </a>
      </td>
      <td>
      {{x.name}}
      </td>
 </tr>