[英]$scope.$watch not working properly
I've got a sign in form which requires a set of errors. 我有一个登录表单,该表单需要一组错误。 Thing is, this sign in form slides in from the left via a custom directive.
事实是,此登录表单通过自定义指令从左侧滑入。 When I want to slide it out of sight I need the current error to disappear.
当我想将其滑出视线时,我需要当前错误才能消失。 I've set a
$watch
function to watch for changes in the sharedInfo.getError()
service function but it only runs when the controller loads and then stops listening for changes. 我已经设置了
$watch
函数来监视sharedInfo.getError()
服务函数中的更改,但是它仅在控制器加载然后停止监听更改时运行。 I can't seem to get this to work, I've used it like this before without any difficulties. 我似乎无法使它正常工作,我以前没有任何困难地使用过它。 I could use some assistance in tracking down the malfunction.
我可以使用一些帮助来查找故障。
The controller: 控制器:
forumApp.controller('signinCtrl', ['$scope', 'fbRef', 'validation', 'userLogic', 'sharedInfo', function($scope, fbRef, validation, userLogic, sharedInfo) {
$scope.$watch('sharedInfo.getError()', function(newValue, oldValue) {
console.log(oldValue);
console.log(newValue);
$scope.error = newValue;
});
$scope.user = {
email: '',
password: ''
}
$scope.validate = function() {
$scope.error = validation.validateSignin($scope.user, $scope.error);
if ($scope.error) {
return false;
}
else {
userLogic.signinUser($scope.user).then(function(authData) {
sharedInfo.setAuthState(authData);
}).catch(function(error) {
switch (error.code) {
case 'INVALID_USER':
$scope.error = 'Invalid email';
sharedInfo.setError($scope.error);
break;
case 'INVALID_EMAIL':
$scope.error = 'Invalid email format';
sharedInfo.setError($scope.error);
break;
case 'INVALID_PASSWORD':
$scope.error = 'Invalid password';
sharedInfo.setError($scope.error);
break;
}
});
}
}
}]);
The service which keeps track of any shared info across the controllers: 跟踪跨控制器共享信息的服务:
forumApp.service('sharedInfo', [function() {
var authState;
var error;
return {
getAuthState: function() {
return authState;
},
setAuthState: function(authData) {
authState = authData;
},
getError: function() {
return error;
},
setError: function(newError) {
error = newError;
}
}
}]);
The directive which performs the slide: 执行幻灯片的指令:
forumApp.directive('mySigninSlide', ['sharedInfo', function(sharedInfo) {
return {
restrict: 'A',
link: function($scope, element, attrs) {
element.on('click', function() {
var sidebar = $('#signin-wrapper');
if ($scope.isAnimated === undefined ||
$scope.isAnimated === false) {
sidebar.stop().animate({left: '340px'});
$scope.isAnimated = true;
}
else {
sidebar.stop().animate({left: '-606px'});
$scope.isAnimated = false;
sharedInfo.setError('');
}
});
}
};
}]);
You have to return the value of what you are watching to be effectively watching it 您必须返回所观看内容的价值,才能有效地观看它
$scope.$watch(sharedInfo.getError(), function(newValue, oldValue) {
console.log(oldValue);
console.log(newValue);
$scope.error = newValue;
});
or 要么
$scope.$watch(function () { return sharedInfo.getError(); },
function(newValue, oldValue) {
console.log(oldValue);
console.log(newValue);
$scope.error = newValue;
});
Another option would be to put sharedInfo
onto the scope. 另一个选择是将
sharedInfo
放到作用域上。
$scope.sharedInfo = sharedInfo;
$scope.$watch('sharedInfo.getError()', function(newValue, oldValue) {
...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.