简体   繁体   English

ng-transclude是否会停止Angular.js中的事件传播?

[英]Does ng-transclude stop event propagation in Angular.js?

The problem occurs when I try to send an event from my directive to a nested directive via the $scope object. 当我尝试通过$ scope对象将事件从指令发送到嵌套指令时,会发生问题。 Eg $scope.broadcast('event') the child directive doesn't receive the event, but when I use the $rootScope object it works eg $rootScope.broadcast('event') . 例如, $scope.broadcast('event')子指令不接收事件,但是当我使用$ rootScope对象时,它起作用,例如$rootScope.broadcast('event')

TL;DR : Why doesn't this work: http://plnkr.co/edit/27qYiHOilpVABSwMI0Fb?p=preview when this works: http://plnkr.co/edit/o91yFKnQzHp7edUTTkJE?p=preview TL; DR :为什么此方法无效: http : //plnkr.co/edit/27qYiHOilpVABSwMI0Fb?p = preview何时有效: http : //plnkr.co/edit/o91yFKnQzHp7edUTTkJE?p=preview

EDIT: In angular.js version 1.3.0 and up this is no longer a issue!!! 编辑:在angular.js版本1.3.0及更高版本中,这不再是问题!!!

Your directive(s) uses ng-transclude . 您的指令使用ng-transclude From the docs: 从文档:

In a typical setup the widget creates an isolate scope, but the transclusion is not a child, but a sibling of the isolate scope. 在典型的设置中,小部件创建隔离范围,但是包含不是子对象,而是隔离范围的同级对象。

This is what your scope hierarchy looks like: 这是您的作用域层次结构:

范围层次

  • 003 is the isolated scope of directive one 003是指令one的隔离范围
  • 004 is the transclusion scope where the two elements live in 004是two元素所在的包含范围
  • 005 is the scope of the first directive two 005是第一个指令的范围two
  • 006 is the transclusion scope of the first directive two 006是第一个指令two的包含范围

So as you can see the scope of two is not a child scope of one . 所以你可以看到的范围two不是子作用域one

Transclusion creates an 'isolate' scope, but the transclusion is not a child, but a sibling of the 'isolate' scope. 包含会创建一个“隔离”作用域,但该包含不是一个孩子,而是“隔离”作用域的同级对象。

(From the source code) (来自源代码)

There is no parent <-> child relationship. 没有父级<->子级关系。 Therefore $broadcast, $emit won't work because $broadcast requires children and $emit requires a parent that you think is a sibling. 因此,$ broadcast,$ emit无效,因为$ broadcast需要孩子,而$ emit需要您认为是同级的父母。

There are a lot of great explanations for the problem here that have probably encouraged you to rethink the way your controllers are organized, as well as how you are using ng-transclude. 对于这个问题,有很多很好的解释,它们可能鼓励您重新考虑控制器的组织方式以及ng-transclude的使用方式。 In case you want another solution, I wrote a tiny directive that solves the interaction between two directives to create the parent <-> child hierarchy. 如果需要其他解决方案,我编写了一个微型指令,该指令解决了两个指令之间的相互作用,以创建父<->子层次结构。 Use at your own risk and I hope this helps! 使用后果自负,希望对您有所帮助! :) :)

https://gist.github.com/meanJim/1c3339bde5cbeac6417d https://gist.github.com/meanJim/1c3339bde5cbeac6417d

It's because you gave the directives isolate scope. 这是因为您给了指令隔离范围。 Remove the scope parameter from the directive if you want it to have nested scope with parent elements. 如果希望该指令的父元素具有嵌套的作用域,请将其从指令中删除。 Or you can define on the scope the properties and values from the parent scope that you would like to pass to the directive's scope. 或者,您可以在范围上定义要传递给指令范围的父范围的属性和值。

http://plnkr.co/edit/aMEpo5zZ76rfMj7Khnoy?p=preview http://plnkr.co/edit/aMEpo5zZ76rfMj7Khnoy?p=preview

If the scope of FirstCtrl is parent to the scope of SecondCtrl , you should use $broadcast method in the FirstCtrl : 如果FirstCtrl的范围是FirstCtrl的范围的父SecondCtrl ,则应在FirstCtrl使用$ broadcast方法:

'use strict';
angular.module('myAppControllers', [])
  .controller('FirstCtrl', function ($scope) {
    $scope.$broadcast('UPDATE_CHILD');
  })
  .controller('SecondCtrl', function ($scope) {
    $scope.$on('UPDATE_CHILD', function() {
      // do something useful here;
    });
  });

if there's no parent/child relation between scopes, you should inject $rootScope into the FirstCtrl and broadcast the event into other controllers (including SecondCtrl) and their corresponding (child in this case) $scope's: 如果范围之间没有父/子关系,则应将$ rootScope注入FirstCtrl并将事件广播到其他控制器(包括SecondCtrl)及其对应的(子级)$ scope的事件中:

'use strict';
angular.module('myAppControllers', [])
  .controller('FirstCtrl', function ($rootScope) {
    $rootScope.$broadcast('UPDATE_ALL');
  });

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

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