ng-repeat with dynamic array:视图未更新

时间:2015-07-01 10:04:43

标签: javascript html angularjs angular-material

我在块内有一些复选框:

<div ng-show="searchIn.IS" class="filterContent">
    <div ng-click="toggleFilter('IS')">
       <span class="{{filters.IS}}FilterIcon"></span> 
       Information System :
    </div>
    <div ng-show="filters.IS">
        <md-checkbox ng-repeat="IS in ISList" ng-click="setISSearch(IS)" ng-checked="isInISSearch(IS)">
            {IS}}
        </md-checkbox>
    </div>
</div>
<br />
<div ng-show="searchIn.area" class="filterContent">
   <div ng-click="toggleFilter('area')">
      <span class="{{filters.area}}FilterIcon"></span>
      Area :
   </div>
   <div ng-show="filters.area">
     <md-checkbox ng-repeat="area in filterAreaList" ng-click="setAreaSearch(area)" ng-checked="isInAreaSearch(area)" ng-show="isInCurrentAreas(area)">
         {{area}}
     </md-checkbox>
   </div>
</div>

所以,每次我检查或取消选中ISList块中的复选框时,都会更改filterAreaList。但视图未更新,所有区域仍然显示。 我还尝试了ng-if而不是ng-show

$scope.filterAreaList = [];

$scope.getAreaFilters = function ()  {
    $scope.searchedIS = $scope.ISList.slice();
    for (var i = $scope.searchedIS.length; i >= 0; i--) {
        if ($scope.searched.IS.indexOf($scope.searchedIS[i]) === -1)
            $scope.searchedIS.splice(i, 1);
    }
    for (var i = 0; i < $scope.areaList.length; i++) {
        for (var j = 0; j < $scope.searchedIS.length; j++)
            if ($scope.hasArea($scope.searchedIS[j], $scope.areaList[i]))
                $scope.filterAreaList.push($scope.areaList[i]);
    }
};

$scope.isInCurrentAreas = function (area) {
    if ($scope.filterAreaList.indexOf(area) === -1)
        return false;
    return true;
};

其他一些主题指出:http://www.bennadel.com/blog/2443-rendering-dom-elements-with-ngrepeat-in-angularjs.htm

但我真的没有看到如何将这个应用到我的案例中。

1 个答案:

答案 0 :(得分:1)

嗯,我实际上错过了一些事情:

为了使其有效,我在$scope.filterAreaList = [];函数中添加了getAreaFilters(),并在getAreaFilters()

中致电setAreaSearch(area)

就是这样。