简体   繁体   English

角度,观察$ scope的变化

[英]Angular, watch $scope for changes

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. 所以我以为,每当$ scope.user更改时,它就会触发,但是似乎只在它第一次从工厂进入控制器时才触发。 Could use some guidance here. 可以在这里使用一些指导。 Thank you! 谢谢!

You 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. 要监视作用域的属性,请将其设置为$ watch函数的第一个参数。 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. 注意:观察对象时,只有在整个对象发生变化时才调用手表,而该对象的属性发生变化时才被调用。 Eg: 例如:

$scope.user = {"username": "john", "email": "john@smith.com"};
$scope.$watch("user", function(user) {
    console.log("users email now is:", user.email);
});

$timeout(function() {
    $scope.user = {"username": "jack", "email": "jack@rocks.com"};
}, 1000);

$timeout(function() {
    $scope.user.email = "jim@beam.com";
}, 2000);

//  after one second console outputs
// jack@rocks.com
//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的第三个参数设置为true。 $watch("property_name", callback_function, 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: jack@rocks.com
users.js:60 user.email watch called with: jim@beam.com
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM