为什么NG隐藏不使用自定义指令工作? [英] Why ng-hide doesn't work with custom directives?

查看:72
本文介绍了为什么NG隐藏不使用自定义指令工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看开发者指南的指示部分上angularjs.org刷新我的知识和获得一些见解,我试图运行的例子之一,但该指令NG隐藏不工作的自定义指令。

在这里的jsfiddle: http://jsfiddle.net/D3Nsk/

 <我-NG对话隐藏=dialogIsHidden上接近=hideDialog()>
  不起作用这里!
< /我-对话框>
< D​​IV NG隐藏=dialogIsHidden>
       它的工作原理在这里。
< / DIV>

这是为什么这个任何想法是怎么回事?

解决方案

看来,标签上的变量dialogIsHidden已经做参考
该指令内,而不是在控制器的变量范围的变量;特定
该指令有它自己的曝晒范围,使这项工作是necesary传递
通过参考所述控制器的指令的变量dialogIsHidden

在这里的jsfiddle:
http://jsfiddle.net/h7xvA/

在变化:

 <我-对​​话框
     NG-隐藏=dialogIsHidden
     上接近=hideDialog()对话框的是隐藏='dialogIsHidden'>

 范围:{
    '关闭':'和;的OnClose,
    dialogIsHidden':'='
  },


解决方案

您正在asigning的对象范围时,创建指令内的孤立范围。这就是为什么$ scope.dialogIsHidden不通过传递到指令,因此该元件不被行经

凯因的建议与使用$父小提琴调整说明了这一点。

I'm reading the directives section of the developers guide on angularjs.org to refresh my knowledge and gain some insights and I was trying to run one of the examples but the directive ng-hide is not working on a custom directive.

Here the jsfiddle: http://jsfiddle.net/D3Nsk/:

<my-dialog ng-hide="dialogIsHidden" on-close="hideDialog()">
  Does Not Work Here!!!
</my-dialog>
<div ng-hide="dialogIsHidden">
       It works Here.
</div>

Any idea on why this is happening?

Solution

Seems that the variable dialogIsHidden on the tag already make a reference to a scope variable inside the directive and not to the variable in the controller; given that the directive has it's own insolated scope, to make this work it's necesary to pass by reference the variable dialogIsHidden of the controller to the directive.

Here the jsfiddle: http://jsfiddle.net/h7xvA/

changes at:

 <my-dialog 
     ng-hide="dialogIsHidden" 
     on-close="hideDialog()" dialog-is-hidden='dialogIsHidden'>

and:

  scope: {
    'close': '&onClose',
    'dialogIsHidden': '='
  },

解决方案

You're creating an isolated scope inside your directive when asigning an object to scope. This is why $scope.dialogIsHidden is not passed through to the directive and thus the element is not being hided.

Kain's suggested adjustment for the fiddle with using $parent illustrates this.

这篇关于为什么NG隐藏不使用自定义指令工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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