0

假设我的祖父组件/指令的范围变量声明如下:$scope.g = { // methods here };

在我的每个嵌套组件/指令的控制器中,我指的是那个变量$scope.$parent.$parent.g,然后调用一些函数,g比如$scope.fromNgClick = function() { $scope.$parent.$parent.g.run(); };. 这很好用(尽管我想要一种更好的方式来指代祖先,例如别名?而不是 $parent 链)。

当我将一个组件从它的祖父组件本地拖到另一个祖父兄弟组件中时(明白了吗?),它$scope.fromNgClick = function() { $scope.$parent.$parent.g.run(); };仍然指向原始范围,而不是我需要的新范围。因此,单击相同的 ng-clickable 元素仍会触发run()前一个祖父母范围内的方法。

这一切都说得通;但是,有没有办法让范围指向新的删除位置祖父母范围?

谢谢!

编辑
标记将类似于以下内容,其中<g-directive>被视为祖父母,因为它transclude在其模板上使用,最终包装子指令:

<g-directive>
    <child-directive></child-directive>
    <another-child-directive></another-child-directive>
    <yet-another-child-directive></yet-another-child-directive>
</g-directive>
<g-directive>
    <c-child-directive></c-child-directive>
    <c-another-child-directive></c-another-child-directive>
    <c-yet-another-child-directive></c-yet-another-child-directive>
</g-directive>

这就是$scope.$parent.$parent.gon 子指令/组件的原因。

可以将子组件拖放到另一个子组件中,<g-directive>但它的控制器仍指向其原始祖父(original<g-directive>的控制器范围变量)。我希望它指向新的祖父母被放入,基本上将它的范围重置为新放置的范围。

4

1 回答 1

0
<g-directive some-attr=1>
 <child-directive></child-directive some-attr=1>
 <another-child-directive></another-child-directive some-attr=1>
 <yet-another-child-directive></yet-another-child-directive some-attr=1>
</g-directive>

<g-directive some-attr=2>
 <child-directive></child-directive some-attr=2>
 <another-child-directive></another-child-directive some-attr=2>
 <yet-another-child-directive></yet-another-child-directive some-attr=2>
</g-directive>

每个都可以有不同的监听器broadcastemit

如果指令重复,ng-repeat$index可以是该属性。希望这可以帮助。

于 2016-07-07T21:28:54.783 回答