在控制器/指令中观察流星集合

时间:2015-09-05 17:42:06

标签: javascript angularjs angular-meteor

这个问题主要是针对AngularJs 1.x

的角度流星包

我试图观看流星收集并在更改时进行一些计算。

angular.module('my-app').controller('MyCtrl', ['$scope', '$meteor', function($scope, $meteor) {
    var filter = {myfield: 10};
    $meteor.subscribe('mycollection', filter);
    $scope.mycollection = $scope.$meteorCollection(Mycollection);

    $scope.$watch('mycollection', function () {
        console.log($scope.mycollection.length)
    });
}]);

但它不起作用。当$scope.mycollection为空时,无论mycollection被更改多少次,watch函数都只会被调用一次。如何观察mycollection中的更改?

1 个答案:

答案 0 :(得分:2)

您需要将object Equality选项设置为true才能设置深度观察器

$scope.$watch('mycollection', function (newVal) {
    console.log($scope.mycollection.length)
}, true); //true for deep watch

如果您不想使用深度观察者,可以使用$watchCollection使用浅观察者,因为如果您使用深度观察者,深度观察者会受到影响。

相关问题