AngularJS - 嵌套的指令没有transclude? [英] AngularJS - nested directives without transclude?

查看:106
本文介绍了AngularJS - 嵌套的指令没有transclude?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

plunker 。

我有一个div属性级别的自定义指令。该指令有一个隔离的范围。里面我的div我有期望能够在父范围的其他指令。

I have an attribute-level custom directive in a div. The directive has an isolated scope. Inside my div I have other directives that expect to be in the scope of the parent.

问题是,我的DIV中的指令只有到隔离范围的访问,而不是父范围即可。我明白为什么,但我不如何解决干净清楚。

The issue is that the directives inside my div have access only to the isolated scope, not to the parent scope. I understand why, but I'm not clear on how to solve it cleanly.

我知道,我可以使用transclude来解决这个(见 plunker ),但这种感觉很马虎。我有没有必要为一个模板,但我不得不创建一个只为transclude工作,并transclude似乎是确保我的嵌套指令访问正确的范围的唯一途径。

I know that I can use transclude to solve this (see plunker) but this feels very sloppy. I have no need for a template, but I'd have to create one just for transclude to work, and transclude seems to be the only way to ensure that my nested directives have access to the correct scope.

是否有另一种选择,更清洁的方式做到这一点?

Is there an alternative, cleaner way to do this?

要阻止一些可能的问题:

To head off some possible questions:


  • 我使用的是属性层次的指令,而不是一个元素级,使事情变得更容易为IE

  • 我使用的是一个孤立的范围,因为这是一个最好的做法 - 我不希望意外软管我的父母范围,我想该指令可以移植

推荐答案

我真的不知道你在做什么的尝试的做。

I'm really not sure what you're trying to do.

但你实际上做的是利用对不良效果的隔离范围的双向绑定。它几乎似乎无关你的问题。

But what you're actually doing is leveraging bidirectional binds on an isolated scope for ill-effect. It almost seems unrelated to your question.

总之...... 是更新了自己的plunker

基本上发生了什么事是你孤立指令里面你需要使用你指定的任何名称在声明范围,在这种情况下 toggleOn()

Basically what was happening is inside of your isolated directive you need to use whatever name you've assigned in your scope declaration, in this case toggleOn().

不过,如果你想给你的可以做到这一点。基本上只需要调用 $ parent.colorToggle()

however if you want to you can do this. Basically just call $parent.colorToggle().

这篇关于AngularJS - 嵌套的指令没有transclude?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆