Javascript 在控制器/指令中观察流星收集
这个问题主要是关于AngularJs 1.x的AngularMeteor包 我正试图观看流星收集,并在它被改变时做一些计算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
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
@非常高兴能帮助您..谢谢:)