获得一次性绑定以用于ng-if [英] Get one-time binding to work for ng-if
问题描述
这个问题已经问过,但是我可以.不能使该解决方案有效,所以我想知道我的具体信息是否有所不同.
This question has been asked before, but I can't get that solution working, so I'm wondering if there's something different in my specifics.
代码非常非常简单:
<h1 ng-if="vm.building.name !== undefined">update {{vm.building.name}}</h1>
如所写,此代码片段工作正常.如果设置了 vm.building.name
,则会显示标记.
As written, this snippet works fine. If vm.building.name
is set, tag appears.
但是,如果我尝试进行一次性绑定,那就是:
However, if I try to make the one-time binding work, that is:
<h1 ng-if="::vm.building.name !== undefined">update {{vm.building.name}}</h1>
标签根本没有出现.我尝试过变体:
The tag doesn't appear at all. I've tried variations:
<h1 ng-if="::(vm.building.name !== undefined)">update {{vm.building.name}}</h1>
仍然没有.这是一件令人沮丧的简单事情.糟糕,该代码似乎可以在 jsfiddle 中使用,但我无法使其在我的特定应用程序中正常工作.某处有设置吗?我需要更改一些琐事吗?
Still nope. It's such a frustratingly simple thing. Hell, the code seems to work in a jsfiddle, but I can't get it working for my particular app. Is there a setting somewhere? Some random bit of trivia I need to change?
如果有的话,我在这里使用角度 1.5.7
.
Is there I'm using angular 1.5.7
, if that matters.
<h1 ng-if="vm.building.name !== undefined">update {{::vm.building.name}}</h1>
也就是说,设置 {{:: vm.building.name}}
最终会给我我想要的结果,但要付出额外的无用观察者的代价,对吗?
That is, setting the {{::vm.building.name}}
ultimately gives me the result I'm looking for, but at the cost of a additional, useless watcher, right?
进一步编辑内容:出于多种原因,这不是一个很好的解决方案.
Further edit on my edit: this isn't a great solution for a number of reasons.
我的问题源于一个事实,即 vm.building.name
等待API调用,因此错过了第一个 $ watch
摘要(我认为).因此,有一种方法可以使 ng-if
在第一次设置 vm.building.name
之后直接执行它的工作,而不是用angular定义的一次性"时间然后退出吗?
My problem stems from the fact that vm.building.name
waits on an API call and therefore misses the first $watch
digest (I think). So instead of "one-time" as defined by angular, is there a way to have ng-if
do its thing directly after vm.building.name
is set the first time and then quit?
推荐答案
好,在这里,您要使用 undefined 检查一次性数据绑定
vm.building.name
值代码>,并且由于它是一次性数据绑定,因此此条件将始终为false.
Well, In here you're checking one-time data binding vm.building.name
value with undefined
and since it is one-time data binding this condition will always be false.
修改您的 ng-if
并使它更简单
<h1 ng-if="::vm.building.name">update {{::vm.building.name}}</h1>
请找到工作的监听器: https://plnkr.co/edit/QA6ZA7e0zHnN45oQWzQH?p=preview
Please find working plunker : https://plnkr.co/edit/QA6ZA7e0zHnN45oQWzQH?p=preview
这可以工作!
干杯!
这篇关于获得一次性绑定以用于ng-if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!