我正在尝试使用控制器中的$scope.$watchCollection
来监视我拥有的服务。最初调用我的控制器时,手表会被调用,但之后每次都不会。
我的控制器:
.controller('LoginModalCtrl', ['$scope', '$modalInstance', '$http', 'loginService', function($scope, $modalInstance, $http, loginService) {
$scope.$watchCollection('loginService.service', function(newVal, oldVal) {
console.log("New Data", newVal);
$scope.service = newVal;
});
$scope.closeDialog = $modalInstance.close;
}])
我的服务:
.service('loginService', ['$http', '$filter', function($http, $filter) {
var service = {
showGDPR: false,
showGDPRModal: false,
invalidCredentials: false
};
return ({
returnVariables: function(){
return service;
},
login: function login(isValid, username, password) { //Called on toggle
//This function changes the values inside service
}
});
}]);
答案 0 :(得分:1)
您在对象上使用 $watchCollection
,仅使用$ watch
$scope.$watch('loginService.service', function(newVal, oldVal) {
console.log("New Data", newVal);
$scope.service = newVal;
});