简体   繁体   English

Angular JS:指令中的范围和ng-repeat

[英]Angular JS: scope and ng-repeat inside a directive

I am trying to write custom directive. 我正在尝试编写自定义指令。 This is a fork from an example by Andy Joslin . 这是Andy Joslin的一个例子。

Please see the code http://plnkr.co/edit/jXfr6P?p=preview 请参阅代码http://plnkr.co/edit/jXfr6P?p=preview

I could pass the selection method to the first child scope, but not further. 我可以将selection方法传递给第一个子范围,但不能进一步。 I know ng-repeat create child scopes, and the original example updates the model correctly. 我知道ng-repeat创建子范围,原始示例正确更新模型。 But, not sure how to call the function. 但是,不知道如何调用该函数。

Click on Apparel -> sel = Apparel ==> works
Click on Mens Shirts  -> sel = Apparel ==> Not OK (needs to be Mens Shirts )
Click on Mens Special Shirts -> sel = Apparel ==> works Not OK (needs to be Mens Special Shirts)
...
Click on Boats -> sel = Boats ==> works

I've managed to fix your example. 我已经成功修复了你的例子。 The key to finding the solution was to change the directive scope to: 找到解决方案的关键是将指令范围更改为:

 scope: {
      tree: '=ngModel',
      selection:'=selection' // changed & to =
    }

as you have to pass the selection function for each tree and choice this way: 因为你必须为每个树传递选择函数并选择这种方式:

selection="selection" // was selection="selection(val)"

See my forked plunker: http://plnkr.co/edit/2tal6V?p=preview 请参阅我的分叉的plunker: http ://plnkr.co/edit/2tal6V?p = preview

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

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