admin管理员组文章数量:1026989
I am trying to watch a scope so I can have a function re run each time, and it does not seem to be working how I immagined it would.
so I just have
$scope.user = userFactory.getUser();
My user being pulled in from a factory
And then later in a click function I change it to something else by having the user click a button like so
$scope.swapUserTest = function(){
$scope.user = {'name' : 'new'}
}
And then I'm trying to just watch if it changes - which right not is just in the click, in the future it will be from multiple differnt things so I would like a watch over it so whenever it changes my function fires. Something like this :
$scope.$watch($scope.user, function() {
console.log("changed")
});
So I was assuming, whenever $scope.user changed this would fire, but it seems to only be firing when it first enters the controller form the factory. Could use some guidance here. Thank you!
I am trying to watch a scope so I can have a function re run each time, and it does not seem to be working how I immagined it would.
so I just have
$scope.user = userFactory.getUser();
My user being pulled in from a factory
And then later in a click function I change it to something else by having the user click a button like so
$scope.swapUserTest = function(){
$scope.user = {'name' : 'new'}
}
And then I'm trying to just watch if it changes - which right not is just in the click, in the future it will be from multiple differnt things so I would like a watch over it so whenever it changes my function fires. Something like this :
$scope.$watch($scope.user, function() {
console.log("changed")
});
So I was assuming, whenever $scope.user changed this would fire, but it seems to only be firing when it first enters the controller form the factory. Could use some guidance here. Thank you!
Share Improve this question asked Jan 29, 2015 at 0:43 ajmajmajmaajmajmajma 14.2k25 gold badges83 silver badges138 bronze badges 1- 3 $scope.$watch('user', function() { – dave Commented Jan 29, 2015 at 0:45
2 Answers
Reset to default 5You can either specify the property of an object:
$scope.$watch('user', function(newValue, oldValue) {
// do something
});
or you can specify a function that returns the value you're watching:
$scope.$watch(function () {
return $scope.user;
}, function(newValue, oldValue) {
// do something
});
To watch a property of a scope, set it as the first parameter for the $watch function. Note: when you watch an object, then your watch gets called only when the entire object changes, not when a property of that object changes. E.g:
$scope.user = {"username": "john", "email": "[email protected]"};
$scope.$watch("user", function(user) {
console.log("users email now is:", user.email);
});
$timeout(function() {
$scope.user = {"username": "jack", "email": "[email protected]"};
}, 1000);
$timeout(function() {
$scope.user.email = "[email protected]";
}, 2000);
// after one second console outputs
// [email protected]
//after 2 seconds ... nothing, even though user object's email has changed.
To "deep watch" an object, set the third parameter of your $watch to be true. $watch("property_name", callback_function, true);
Or when you want to watch a specific property of an object, use
$scope.$watch("user.email", function(email) {
console.log("user.email watch called with:", email);
});
/*
outputs both
user.email watch called with: [email protected]
users.js:60 user.email watch called with: [email protected]
*/
I am trying to watch a scope so I can have a function re run each time, and it does not seem to be working how I immagined it would.
so I just have
$scope.user = userFactory.getUser();
My user being pulled in from a factory
And then later in a click function I change it to something else by having the user click a button like so
$scope.swapUserTest = function(){
$scope.user = {'name' : 'new'}
}
And then I'm trying to just watch if it changes - which right not is just in the click, in the future it will be from multiple differnt things so I would like a watch over it so whenever it changes my function fires. Something like this :
$scope.$watch($scope.user, function() {
console.log("changed")
});
So I was assuming, whenever $scope.user changed this would fire, but it seems to only be firing when it first enters the controller form the factory. Could use some guidance here. Thank you!
I am trying to watch a scope so I can have a function re run each time, and it does not seem to be working how I immagined it would.
so I just have
$scope.user = userFactory.getUser();
My user being pulled in from a factory
And then later in a click function I change it to something else by having the user click a button like so
$scope.swapUserTest = function(){
$scope.user = {'name' : 'new'}
}
And then I'm trying to just watch if it changes - which right not is just in the click, in the future it will be from multiple differnt things so I would like a watch over it so whenever it changes my function fires. Something like this :
$scope.$watch($scope.user, function() {
console.log("changed")
});
So I was assuming, whenever $scope.user changed this would fire, but it seems to only be firing when it first enters the controller form the factory. Could use some guidance here. Thank you!
Share Improve this question asked Jan 29, 2015 at 0:43 ajmajmajmaajmajmajma 14.2k25 gold badges83 silver badges138 bronze badges 1- 3 $scope.$watch('user', function() { – dave Commented Jan 29, 2015 at 0:45
2 Answers
Reset to default 5You can either specify the property of an object:
$scope.$watch('user', function(newValue, oldValue) {
// do something
});
or you can specify a function that returns the value you're watching:
$scope.$watch(function () {
return $scope.user;
}, function(newValue, oldValue) {
// do something
});
To watch a property of a scope, set it as the first parameter for the $watch function. Note: when you watch an object, then your watch gets called only when the entire object changes, not when a property of that object changes. E.g:
$scope.user = {"username": "john", "email": "[email protected]"};
$scope.$watch("user", function(user) {
console.log("users email now is:", user.email);
});
$timeout(function() {
$scope.user = {"username": "jack", "email": "[email protected]"};
}, 1000);
$timeout(function() {
$scope.user.email = "[email protected]";
}, 2000);
// after one second console outputs
// [email protected]
//after 2 seconds ... nothing, even though user object's email has changed.
To "deep watch" an object, set the third parameter of your $watch to be true. $watch("property_name", callback_function, true);
Or when you want to watch a specific property of an object, use
$scope.$watch("user.email", function(email) {
console.log("user.email watch called with:", email);
});
/*
outputs both
user.email watch called with: [email protected]
users.js:60 user.email watch called with: [email protected]
*/
本文标签: javascriptAngularwatch scope for changesStack Overflow
版权声明:本文标题:javascript - Angular, watch $scope for changes - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744968779a2127010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论