0

这个问题主要是关于 AngularJs 1.x 的 angular-meteor 包

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

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)
    });
}]);

但它不起作用。watch 函数只会在$scope.mycollection为空时调用一次,无论 mycollection 已更改多少次。如何观察变化mycollection

4

1 回答 1

2

您需要将 object Equality 选项设置为 true 以设置深度观察者

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

如果你不想使用 deep watcher,你可以使用 shallow watcher using $watchCollection,因为 deep watcher 会影响你是否使用 deep watcher。

于 2015-09-05T17:43:31.553 回答