2017-06-02 79 views
0

我使用PHP循环浏览需要在我的页面上过滤的数据,然后使用ng-hide隐藏和显示基于控制器中过滤器数组的数据。计算隐藏元素

但是,当我尝试创建一些结果脚本时,它似乎只能得到之前显示的结果数(因此,如果我以3开头,然后单击仅显示2个结果的内容,则会显示结果3)。

/* Watch for changes in the Filter JSON array. */ 
    $scope.$watchCollection('Filter', function(){ 
     $scope.filterResults = $(".compContOuter").not(".ng-hide").length; 
    }); 

    /* Get the results shown upon page load */ 
    $scope.filterResults = $(".compContOuter").not(".ng-hide").length; 

有是有ng-bind='filterResults'显示的div的长度范围标记。

示例图像:

enter image description here

+0

请不要在您的控制器中使用DOM选择器,而要使用对象 – devqon

+0

您可以详细说明为什么@devqon? –

+0

由于很多原因:你没有使用角度的约束力,这将很容易解决你的问题;当你处理这个dom的时候,你需要调用一个$摘要;如果明天你使用ng-if,你将不得不更新你的控制器(维护问题);使用太多的$观察者可能会减慢你的页面。 – Galhem

回答

0
//if you play with watch then for you have to destroy it also so it will 
//make performance boost and make angular digest life cycle easy..hope it 
//make sense 

var filterWatch = $scope.$watchCollection('Filter', function(){ 
    $timeout(function(){ 
     $scope.filterResults = $(".compContOuter").not(".ng-hide").length; 
    }); 
}); 

//Destroy watch here 
$scope.$on('$destroy', filterWatch); 
+0

这没有奏效,对不起。 –

+0

而不是$超时,我用$间隔并解决了问题。 –

0

肮脏的方式: 通常,这些类型的延迟问题,可以通过添加在你的代码$超时是固定的,像

$scope.$watchCollection('Filter', function(){ 
    $timeout(function(){ 
     $scope.filterResults = $(".compContOuter").not(".ng-hide").length; 
    }); 
}); 

我'不明白的是,你已经有每个过滤器的括号内的数字,不能被分配给标题?

+0

这没有奏效,对不起。 –

0

正如其他人所建议的,你不应该在你的控制器中这样做,你应该避免一起使用jQuery。

但是,您的问题是由$scope未被更新引起的。您可以将$scope.$applyAsync()添加到$watchCollection回调中。例如:

$scope.$watchCollection('Filter',() => { 
    $scope.filterResults = $(".compContOuter").not(".ng-hide").length; 
    $scope.$applyAsync(); 
}); 

这将强制$ scope的摘要循环。

+0

对不起,这没有奏效。 –

+0

我不确定那可能是什么问题。 –