简体   繁体   English

角度手表未开除

[英]angular watch not fired

There is 1 angular app, with 1 parent controller, and a child controller. 有1个角度应用程序,带有1个父控制器和一个子控制器。 In the child, there is 1 $watch WATCH-CHILD for OBJ-CHILD, which triggers an $emit. 在孩子中,有1个$ watch WATCH-CHILD for OBJ-CHILD,这会触发一个$ emit。 In the parent, there is a listener for the $emit, we'll call it ON-LISTENER, and a $watch WATCH-PARENT for OBJ-PARENT (which uses true as the 3rd argument). 在父级中,有一个$ emit的侦听器,我们将其称为ON-LISTENER,还有一个OBJ-PARENT的$ watch WATCH-PARENT(使用true作为第三个参数)。

When the child's OBJ-CHILD is changed, it triggers WATCH-CHILD, which triggers the $emit. 更改孩子的OBJ-CHILD时,它将触发WATCH-CHILD,这将触发$ emit。 The parent listener ON-LISTENER is fired, and changes OBJ-PARENT. 父侦听器ON-LISTENER被激发,并更改OBJ-PARENT。 It also sets some $location properties. 它还设置了一些$ location属性。 The $watch WATCH-PARENT for OBJ-PARENT is never fired (even though the value has changed), as well as the properties set on $location not changed in the browser URL (I know they are indeed changed inside the JavaScript, cause I print them). OBJ-PARENT的$ watch WATCH-PARENT不会被触发(即使值已更改),以及$ location上设置的属性在浏览器URL中没有更改(我知道它们确实在JavaScript中已更改,因为我打印)。

In order to make sure that ON-LISTENER is called within a $digest, I tried to call $digest at the end of ON-LISTENER, and got the expected exception. 为了确保在$ digest中调用ON-LISTENER,我尝试在ON-LISTENER的末尾调用$ digest,并得到了预期的异常。

Any idea if I'm doing something wrong? 知道我做错了什么吗? I expect the changes that occur in ON-LISTENER to trigger WATCH-PARENT and browser URL change. 我希望ON-LISTENER中发生的更改触发WATCH-PARENT和浏览器URL更改。

I will try to reproduce on jsfiddle and edit this post if successful. 如果成功,我将尝试在jsfiddle上重现并编辑该帖子。

The code looks like: 代码如下:

CHILD: 儿童:

$scope.$watch('vars.model', function(newValue, oldValue) {
  console.log('model changed');
  $scope.$emit('highlightChange', newValue);
}, true);

PARENT: 家长:

$scope.$watch('vars.model.highlight', function(newValue, oldValue) {
  console.log('highlight changed');
}, true);

$scope.$on('highlightChange', function(event, value) {
  console.log('listener', $scope.vars.model.highlight.categoryId, value.categoryId);
  $location.search('category-id', value.categoryId);
  $scope.vars.model.highlight.categoryId = value.categoryId;
}

Next time please provide more code which works, that way you can get better answers. 下次,请提供更多有效的代码,这样您可以获得更好的答案。

Here is a Demo plunker which I created to test the code which you provided. 这是我创建的一个演示插件 ,用于测试您提供的代码。 It works just fine. 它工作正常。 If you could provide more code then we could find the real reason why it did not work. 如果您可以提供更多代码,那么我们可以找到导致其无效的真正原因。

I created two controllers parentCtrl and childCtrl which uses your code and object of provided structure. 我创建了两个控制器parentCtrlchildCtrl ,它们使用您的代码和所提供结构的对象。

$scope.vars = {
   model:{
      highlight:{
        categoryId : 5 //This value is set for testing purposes
      }
   }
};

Also, I changed watch target ( vars.model -> vars.model.highlight ) to be the same as in parent controller 另外,我将监视目标( vars.model- > vars.model.highlight )更改为与父控制器中的目标相同

$scope.$watch('vars.model.highlight', function(newValue, oldValue) {
  console.log('child model changed (new/old)', newValue, oldValue);
  $scope.$emit('highlightChange', newValue);
  console.log('Emited change event');
}, true);

Thanks for the help. 谢谢您的帮助。 I found out that my event originated from a manual call to $scope.$digest() due to the originating event being triggered from a daterangepicker 'apply.daterangepicker' event. 我发现我的事件源于对$ scope。$ digest()的手动调用,这是由于原始事件是由daterangepicker'apply.daterangepicker'事件触发的。 When I changed that to $scope.$apply, the problem seemed to go away. 当我将其更改为$ scope。$ apply时,问题似乎消失了。 I can assume from that, that $apply() is the one in charge of keep calling $watch as long as there are changes, and that $digest() doesn't do so. 由此我可以假定,只要有更改,$ apply()就是负责继续调用$ watch的负责人,而$ digest()不会这样做。

For future reference, I placed the problem here: https://plnkr.co/edit/ItkALhw16Aqukk3EFrRz?p=preview 为了将来参考,我将问题放在这里: https : //plnkr.co/edit/ItkALhw16Aqukk3EFrRz?p=preview

$scope.digest();

should become 应该成为

$scope.apply();

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

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