简体   繁体   English

如何否定AngularJS ng-if指令的参数?

[英]How do I negate the parameter for AngularJS ng-if directive?

Quick Example: 快速示例:

There is a routed parameter (/Home/:isLoggedIn) that equates to true or false. 有一个路由参数(/ Home /:isLoggedIn)等于true或false。 (/Demo/#/Home/false) and a controller property (/ Demo /#/ Home / false)和控制器属性

this.loggedIn = this.routeParams.loggedIn; this.loggedIn = this.routeParams.loggedIn;

I have a view (Home.html) that has two elements, each with an ng-if attribute. 我有一个视图(Home.html)有两个元素,每个元素都有一个ng-if属性。

<div ng-if="home.loggedIn">
    Logged In!
</div>
<div ng-if="!home.loggedIn">
    Not Logged In...
</div>

If I navigate to /Demo/#/Home/true then the first element displays and the second does not. 如果我导航到/ Demo /#/ Home / true,则第一个元素显示,第二个元素不显示。 If I navigate to /Demo/#/Home/false then the first element does not display NOR does the second one. 如果我导航到/ Demo /#/ Home / false,则第一个元素不显示NOR执行第二个元素。

I would expect the !home.loggedIn parameter evaluate to true when the value of loggedIn is, in fact, false. 当loggedIn的值实际上为false时,我希望!home.loggedIn参数计算为true。

Any advice here? 有什么建议吗?

It is quite obvious that he problem has its root to the fact that routeParams.loggedIn is a string. 很明显,他的问题routeParams.loggedIn是一个字符串。

So the solution is quite obvious: 所以解决方案很明显:

// Change that:
this.loggedIn = this.routeParams.loggedIn;

// To this:
this.loggedIn = this.routeParams.loggedIn === 'true';

But why the weird behaviour ? 但为什么奇怪的行为呢?
Why work not showing anything when loggedIn is 'false' ? loggedIn为'false'时,为什么工作没有显示任何内容?

Well, here is why: 那么,原因如下:

The ngIf directive uses the following toBoolean() function to convert its value to boolean: ngIf指令使用以下toBoolean()函数将其值转换为boolean:

function toBoolean(value) {
  if (typeof value === 'function') {
    value = true;
  } else if (value && value.length !== 0) {
    var v = lowercase("" + value);
    value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
  } else {
    value = false;
  }
  return value;
}

If a string is passed to toBoolean() it converts it to lowercase and checks (among other things) if it equals 'false' (in which case it returns false ). 如果将一个字符串传递给toBoolean()它会将其转换为小写并检查(除其他外)是否等于'false'(在这种情况下它返回false )。 This is different than the default JavaScript implementation which interprets any non-empty string as true when casting to boolean. 这与默认的JavaScript实现不同,后者在转换为boolean时将任何非空字符串解释为true

So, let's examine the two cases for both ngIf s: 那么,让我们检查两个ngIf的两种情况:

  1. loggedIn === 'true'

    ngIf1 evaluates home.loggedIn --> 'true' (string) ngIf1评估home.loggedIn - >'true'(字符串)
    ngIf1 passes this value through toBoolean() ngIf1将此值传递toBoolean()
    toBoolean('true') returns true (because it sees a string that can't match with any string considered falsy) toBoolean('true')返回true (因为它看到的字符串与任何被认为是falsy的字符串都不匹配)
    ngIf1 renders its content ngIf1呈现其内容

    ngIf2 evaluates !home.loggedIn <=> !'true' --> false (boolean) ngIf2评估!home.loggedIn <=> !'true' - > false(布尔值)
    (this happens because any non-empty string happens to evaluate to true) (这是因为任何非空字符串恰好评估为true)
    ngIf2 passes this value through toBoolean() ngIf2将此值传递toBoolean()
    toBoolean(false) returns false toBoolean(false)返回false
    ngIf2 does not render its content ngIf2不呈现其内容

  2. loggedIn === 'false'

    ngIf1 evaluates home.loggedIn --> 'false' (string) ngIf1计算home.loggedIn - >'false'(字符串)
    ngIf1 passes this value through toBoolean() ngIf1将此值传递toBoolean()
    toBoolean('false') returns false (because it sees a string that is considered falsy toBoolean('false')返回false (因为它看到一个被认为是的字符串
    ngIf1 does not render its content ngIf1不呈现其内容

    ngIf2 evaluates !home.loggedIn <=> !'false' --> false (boolean) ngIf2评估!home.loggedIn <=> !'false' - > false(布尔值)
    (this happens because any non-empty string happens to evaluate to true) (这是因为任何非空字符串恰好评估为true)
    ngIf2 passes this value through toBoolean() ngIf2将此值传递toBoolean()
    toBoolean(false) returns false toBoolean(false)返回false
    ngIf2 does not render its content ngIf2不呈现其内容

So, this explains the "weird" behaviour (hopefully in an understandable way). 所以,这解释了“怪异”的行为(希望以一种可以理解的方式)。

Problem is likely home.loggedIn is a string, when passed to ng-if it is probably evaluating and doing the conversion from string to bool to get the value "false" into false. 问题可能是home.loggedIn是一个字符串,当传递给ng时 - 如果它可能正在评估并执行从string到bool的转换以将值“false”变为false。 In the expression evaluation before the value is passed through if you have !"false" that is actually false since any string is true, negating it becomes false. 在值传递之前的表达式求值中如果你有!“false”实际上是假的,因为任何字符串都为真,否则它变为false。

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

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