简体   繁体   English

可能有多个条件的Angular ng-if,有的绑定一次,有的双向?

[英]Possible to have Angular ng-if with multiple conditions, some bound once, some two-way?

My question is not whether or not you can use multiple conditions in ng-if , but rather whether or not it's possible to have ng-if with only some conditions bound once, while others watch for updates? 我的问题不是您是否可以在ng-if使用多个条件,而是是否可以仅将某些条件绑定一次ng-if ,而其他条件则监视更新?

As a simple example, if I want to bind the name once, but have a toggle state property that I want to watch for updates: <span ng-if="::page.name === 'something' && page.viewEnabled">Correct page, and the view is enabled!</span> 举一个简单的例子,如果我想一次绑定名称,但是有一个我想要监视更新的切换状态属性: <span ng-if="::page.name === 'something' && page.viewEnabled">Correct page, and the view is enabled!</span>

I would expect that a toggle to page.viewEnabled assuming it's a boolean, would cause the span to disappear. 我希望假设它是布尔值,切换到page.viewEnabled会导致跨度消失。 That does not appear to be the case (using Angular 1.3.11). 似乎并非如此(使用Angular 1.3.11)。

Does the one-time binding on ::page.name setup the entire expression as being bound once? ::page.name上的一次性绑定是否将整个表达式设置为一次绑定?

Thanks in advance! 提前致谢!

EDIT: What I'm curious about is if you have multiple conditions in the same ng-if , one being bound once, while another is not. 编辑:我很好奇的是,如果您在同一个ng-if有多个条件,一个条件被绑定一次,而另一个则没有。 What I'm seeing is that if you have one that is bound once, the other changing will not affect the ng-if statement. 我看到的是,如果您有一个绑定一次,则其他更改不会影响ng-if语句。

I just tested this out, and can confirm that you can in fact bind-once to an ngIf statement. 我刚刚对其进行了测试,并且可以确认您实际上可以bind-oncengIf语句。 Consider the following example: 考虑以下示例:

https://plnkr.co/edit/m32BBBYlNffxTfggb33g?p=preview https://plnkr.co/edit/m32BBBYlNffxTfggb33g?p=preview

Say your UI is programmatically updating an array of fruits , and let's say we have two ngIf expressions: one uses bind-once , the other is bound normally (ie twice): 假设您的用户界面正在以编程方式更新fruits数组,并且我们有两个ngIf表达式:一个使用bind-once ,另一个使用正常绑定(即两次):

<div ng-if="::fruits.length > 3">More than three fruits!</div>
<div ng-if="fruits.length > 3">More than three fruits!</div>

If we begin adding to this array, we'll note that only the second ngIf expression is triggered when the condition is met: 如果我们开始添加到此数组,我们将注意到,满足条件时仅触发第二个ngIf表达式:

在此处输入图片说明

UPDATE 更新

So it looks as though the original question was more geared toward putting two (2) expressions in an ngIf , but where one expression utilizes bind-once . 因此,似乎原始问题更适合于在ngIf放置两(2)个表达式,但是其中一个表达式使用bind-once And interestingly enough, neither of the statements fire (see below), which is a bit surprising, which would seem to indicate you cannot combine one and two-way bound expressions. 有趣的是,这两个语句都没有触发(请参见下文),这有点令人惊讶,这似乎表明您无法组合单向和双向绑定表达式。 In the first ngIf , I would have expected the very first condition to have been evaluated truthfully, then short-circuit since it doesn't need to evaluate the second expression, which happens to be bound once. 在第一个ngIf ,我希望对第一个条件进行如实的评估,然后将其短路,因为它不需要评估第二个表达式,后者恰好被绑定一次。 But that doesn't seem to be the case. 但这似乎并非如此。

Plunkr has been updated. Plunkr已更新。

  <div ng-if="fruits.length > 3 || ::fruits.length > 3">1.) More than three fruits!</div>
  <div ng-if="::fruits.length > 3 || fruits.length > 3">2.) More than three fruits!</div>

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

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