简体   繁体   English

AngularJS,$ watch运行两次,无法使旧的/新的值比较工作

[英]AngularJS, $watch running twice, can't get old/new value comparison to work

I'm using AngularJS and I want to watch if a variable changes, and the decrease another variable. 我正在使用AngularJS,我希望观察变量是否发生变化,以及减少另一个变量。 Here's what I have: 这就是我所拥有的:

$scope.$watch('watchMe', function () {
    $scope.decreaseMe--;
)

The watchMe variable is available to all scopes. watchMe变量适用于所有范围。 I only have one controller which contains both a function where watchMe is changed as well as the watch. 我只有一个控制器,它既包含watchMe更改的功能,也包含手表。

Watch is running twice each time watchMe is changed and I understand that this is the way Angular works. WatchMe每次更改时都会运行两次,我知道这是Angular的工作方式。 However, that means that decreaseMe will be wrong, since it will be decreased twice as well. 但是,这意味着reduceMe将是错误的,因为它将减少两次。

I've tried comparing old and new values but I can't get it to work. 我试过比较旧的和新的价值观,但我无法让它发挥作用。

I'm outputting old/new/decreased values and this is what I get when watchMe is changed once: 我正在输出旧的/新的/减少的值,这是我在watchMe更改一次时获得的:

old: 7  new: 8 
decreasing  5
old: 7  new: 8 
decreasing  4 

What should I do to decrease decreaseMe only once when watchMe is changed? 当watchMe改变时,我应该怎样做才能减少reduceMe?


Update: 更新:

Tried to make a Plunkr to show this but in Plunkr $watch only fires once.. 试图让一个Plunkr展示这个,但在Plunkr $手表只发射一次..

Is this not working? 这不起作用吗?

$scope.$watch('watchMe', function( newVal, oldVal ) {
    if( newVal != oldVal ) {
        $scope.decreaseMe--;
    }
});

You should share a lot more code so we can see how you are set up. 您应该共享更多代码,以便我们了解您的设置方式。 In any case, I've run into a similar problem before when I referenced the AngularJS lib twice on my page. 无论如何,在我在页面上两次引用AngularJS库之前,我遇到了类似的问题。

I have run into a similar issue when using directive templates with a shared controller. 在将指令模板与共享控制器一起使用时,我遇到了类似的问题。 This means that your controller code gets run twice. 这意味着您的控制器代码会运行两次。

(Try outputting to the console in your controller but outside your watch statement to prove this, you should see a duplicate) (尝试输出到控制器中的控制台,但在监视声明之外为了证明这一点,你应该看到重复)

.directive('directiveOne', function () {
    return {
        templateUrl: 'app/templates/directive-one.html',
        restrict: 'A',
        controller: 'directiveController'
    };
})
.directive('directiveTwo', function() {
    return {
        templateUrl: 'app/templates/directive-two.html',
        restrict: 'A',
        controller: 'directiveController'
    };
})

The solution was to separate them out into their own controller 解决方案是将它们分离到自己的控制器中

EG 例如

.directive('directiveOne', function () {
    return {
        templateUrl: 'app/templates/directive-one.html',
        restrict: 'A',
        controller: 'directiveOneController'
    };
})
.directive('directiveTwo', function() {
    return {
        templateUrl: 'app/templates/directive-two.html',
        restrict: 'A',
        controller: 'directiveTwoController'
    };
})

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

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