Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在控制器/指令中观察流星收集_Javascript_Angularjs_Angular Meteor - Fatal编程技术网

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

Javascript 在控制器/指令中观察流星收集,javascript,angularjs,angular-meteor,Javascript,Angularjs,Angular Meteor,这个问题主要是关于AngularJs 1.x的AngularMeteor包 我正试图观看流星收集,并在它被改变时做一些计算 angular.module('my-app').controller('MyCtrl', ['$scope', '$meteor', function($scope, $meteor) { var filter = {myfield: 10}; $meteor.subscribe('mycollection', filter); $scope.my

这个问题主要是关于AngularJs 1.x的AngularMeteor包

我正试图观看流星收集,并在它被改变时做一些计算

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

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

要设置deep watcher,需要将object Equality选项设置为true

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

如果您不想使用deep watcher,您可以使用shallow watcher,因为如果您使用deep watcher,deep watcher将对您产生影响。

谢谢,这是一项工作!但是,哪一个更好或等效?@致命如果您想对
mycollection
对象的任何属性更改触发一个手表,而该属性更改可能位于非常深的级别,那么请使用
$watch
true
选项,该选项指示深度手表。。但是如果您对第一级元素感兴趣,请使用
$watchCollection
@非常高兴能帮助您..谢谢:)