(改变)angular2事件挂接 [英] (change) event hook in angular2

查看:171
本文介绍了(改变)angular2事件挂接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道了(其他城市)事件绑定在angular2,但我觉得很奇怪,为什么预期我的code不工作?
我的code是这里..

http://plnkr.co/edit/9pSWSeqBc5oaSAtsfwNY?p=$p$ PVIEW

在更改事件称为不是两个条件的作品期待首次

(其他城市)=长假=假日&安培;!&安培;!=员工员工

在更改事件调用它第一次工作正常,但第二次将只对第一个条件,即节日在我的例子,而我所期待的是这两个值是真的还是假的,但没有达到预期。
肯定有一些错误是有任何人谁解释的生命周期(其他城市)事件是否正确?


解决方案

您应该将它们与分离; 而不是&放大器;&安培; 运营商。

如果第一八佰伴pression出了问题,也不会评价下一个前pression。

 (其他城市)=长假=假日;!=员工员工


  

如何&放大器;&放大器; 运营商的作品


  
  

      
  1. 假设 A和&安培; b ,如果两者真的那么只有返回true(总之,不应该有任何值否则将返回false)。

  2.   
  3. 当评估 A和&安培; b code会首先检查 A 值为真正则只有间preTER意志去 b 的价值进行评估。如果 A 它的价值是自我
      那么它不评价(检查) B 的价值,而 A&安培;&安培; b
      前pression将返回

  4.   

在您遇到假期=假日&安培;!&安培;员工=!员工。在初始加载两个节日&安培; 员工值。当你点击复选框它评估假期=假日&安培;!&安培; !=员工员工,无论节日&安培; 员工值为真正

基本上幕后当1 节日=!节日得到评估,节日变成真正&安培; 节日=!节日前pression返回最新值(返回真正),无论是第二前pression做同样的事情和放大器;返回真正

现在假期= TRUE &安培; 员工= TRUE 。当你点击复选框一次。它要求改变事件&安培;再次尝试评估假期=假日&安培;!&安培;员工=!员工
其中,节日=!节日收益,所以后来我提到上述如何 &功放;&放大器; 运营商的作品。它不关心前pression的下一部分,并返回值。

现在假期= FALSE &安培; 员工= TRUE 。如果您在复选框再次单击,然后节日真可谓真正&安培;继续评估前pression的另一部分由员工真可谓真正

i know about the (change) event Binding in angular2 but i am surprised why my code is not working as expected ? my code is here..

http://plnkr.co/edit/9pSWSeqBc5oaSAtsfwNY?p=preview

when change event called not both condition works expect first time

(change)="holiday= !holiday && employee= !employee"

when change event calls first time it works fine but second time it will work only for first condition i.e holiday in my example rather what i expected is both the value is either true or false but not as expected. surely there is some mistake is there anyone who explain the life cycle of (change) event properly ?

解决方案

You should separate them with ; instead of && operator.

If first expression goes wrong, it would not evaluate the next expression.

(change)="holiday= !holiday;employee= !employee"

How && operator works?

  1. Suppose a && b, if both are true then only it returns true(in short there shouldn't be any false value otherwise it will return false).
  2. When evaluating a && b code will first check a value is true then only the interpreter will goes b value to evaluate it. If a value it self is false then it don't evaluate(check) b's value, and a && b expression will return false.

Before you were having holiday= !holiday && employee= !employee. On initial load both holiday & employee has false value. When you click on the checkbox it evaluates holiday= !holiday && employee= !employee, both holiday & employee value becomes true.

Basically behind the scene when 1st holiday= !holiday get evaluated, holiday becomes true to false & holiday= !holidayexpression return latest value(returns true), whether 2nd expression does the same thing & returns true.

Now holiday = true & employee = true. When you click on the check box again. It calls change event & again try to evaluate holiday= !holiday && employee= !employee. Where holiday= !holiday return false, so then as I mention above How && operator works?. It doesn't care about the next part of expression, and returns false value.

Now holiday = false & employee = true. If you click on checkbox again, then holiday turns out to true & proceed to evaluate other part of expression by which employee turns out to true from false.

这篇关于(改变)angular2事件挂接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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