我可以把ng-if指令放在一个也绑定到控制器的元素上 [英] Can I put ng-if directive on an element which also binds to a controller

查看:125
本文介绍了我可以把ng-if指令放在一个也绑定到控制器的元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我有一个控制器和ng-if指令与

I was wondering in case I have a controller and ng-if directive on the same element as in

<div foo ng-if="ctrl.visible">You can see me</div>

和控制器,例如

NgController(selector: ‘[foo]’,….)
class FooController { var visible = true; }

我应该看到文字你可以看到我吗?

Should I see the text "You can see me" or not?

推荐答案

这里是答案,我不会看到的文本基本上, ng-if transcluding指令,这意味着整个元素被从DOM中删除,没有其他指令被实例化,直到ng - 如果实例化块,但不会发生,因为 ctrl.visible 是从来没有在范围上发布,所以它总是falsy ...鸡和蛋的问题。其实,甚至可能更糟: ctrl 可以是父控制器,如果控制器有可见字段,它可能导致不可预测的行为。

Here's the answer. I would not see the text. Basically, ng-if is a transcluding directive which means that the entire element is ripped out of the DOM and no other directives are instantiated until ng-if instantiates the Block, but that never happens because ctrl.visible is never even published on the scope, so it's always falsy... chicken and egg problem. Actually, it can be even worse: ctrl could be the parent controller, and if that controller happen to have visible field it can cause unpredictable behavior.

这篇关于我可以把ng-if指令放在一个也绑定到控制器的元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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