【问题标题】:access child scope property in another child in angular js在angular js中访问另一个孩子的子范围属性
【发布时间】:2013-12-02 14:58:59
【问题描述】:

是否可以在 Angular js 中访问兄弟属性?我有一个父母和两个孩子

我的父母是我的父母 div

孩子们是:-

1. <li ng-repeat= "item in list"/>

2. and within ng-repeat i have a custom directive  <directive1></directive1>

根据项目类型,可能会有不同的指令,所以我将项目中的指令作为属性获取

i.e. item.directive= "<div directive1> </directive>"

我编译了上面的html,这样directive1的代码就会被调用!

现在我如何访问指令 1 的链接功能中的“项目”?

链接:函数有作用域,它有一个属性 $$childTail 这向我展示了属于 ng-repeat 子作用域的 item!推荐使用它吗?还是有解决办法?谢谢!

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat


    【解决方案1】:

    您可以将作用域变量传递到指令中并在链接函数中使用它们。

    <div directive some-var="item"></div>
    

    确保在您的指令中指定范围。

    scope: {
      someVar: '='
    },
    link: funciton (scope, element, attr) {
       // outputs the contents of 'item' from the parent scope.
       console.log(scope.someVar);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多