【问题标题】:Access parent directive's controller scope variables in child directive在子指令中访问父指令的控制器范围变量
【发布时间】:2015-12-08 08:30:03
【问题描述】:

我有两个指令,即“父”和“子”。我想根据父指令控制器中范围变量的值设置子指令的可见性。

<div>
    <h1>Parent Directive selected index : {{ selectedIndex }}</h1>
    <div id="children" ng-transclude></div>
</div>

父指令的控制器在其范围内具有selectedIndex 变量,而子指令在其范围内具有id 变量。我正在尝试在子指令上使用ng-show

<div ng-show="id==selectedIndex">
  <h1>{{id}}</h1>
</div>

它不能正常工作。

我附上了场景的 jsfiddle 链接。

https://jsfiddle.net/vibhanshu/pep44qz7/10/

【问题讨论】:

  • 你忘了""这里
  • 这个问题已经被这个post回答了

标签: javascript angularjs angularjs-directive


【解决方案1】:

您可以在子指令selectedIndex: '=' 上定义双向属性

scope: { id: '@', selectedIndex: '='}

<parent>
      <child id="1" selected-index="selectedIndex"></child>
      <child id="2" selected-index="selectedIndex"></child>
</parent>

更新小提琴 - https://jsfiddle.net/pep44qz7/12/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多