(改变)angular2事件挂接 [英] (change) event hook in angular2
问题描述
我知道了(其他城市)事件绑定
在angular2,但我觉得很奇怪,为什么预期我的code不工作?
我的code是这里..
http://plnkr.co/edit/9pSWSeqBc5oaSAtsfwNY?p=$p$ PVIEW
在更改事件称为不是两个条件的作品期待首次
(其他城市)=长假=假日&安培;!&安培;!=员工员工
在更改事件调用它第一次工作正常,但第二次将只对第一个条件,即节日
在我的例子,而我所期待的是这两个值是真的还是假的,但没有达到预期。
肯定有一些错误是有任何人谁解释的生命周期(其他城市)
事件是否正确?
您应该将它们与分离;
而不是&放大器;&安培;
运营商。
如果第一八佰伴pression出了问题,也不会评价下一个前pression。
(其他城市)=长假=假日;!=员工员工
如何
&放大器;&放大器;
运营商的作品
- 假设
A和&安培; b
,如果两者真的那么只有返回true(总之,不应该有任何假
值否则将返回false)。
- 当评估
A和&安培; b
code会首先检查A
值为真正
则只有间preTER意志去b
的价值进行评估。如果A
它的价值是自我
假
那么它不评价(检查)B
的价值,而A&安培;&安培; b
前pression将返回假
。
块引用>在您遇到
假期=假日&安培;!&安培;员工=!员工
。在初始加载两个节日
&安培;员工
的假
值。当你点击复选框
它评估假期=假日&安培;!&安培; !=员工员工
,无论节日
&安培;员工
值为真正
。基本上幕后当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?
- Suppose
a && b
, if both are true then only it returns true(in short there shouldn't be anyfalse
value otherwise it will return false).- When evaluating
a && b
code will first checka
value istrue
then only the interpreter will goesb
value to evaluate it. Ifa
value it self isfalse
then it don't evaluate(check)b
's value, anda && b
expression will returnfalse
.
Before you were having
holiday= !holiday && employee= !employee
. On initial load bothholiday
&employee
hasfalse
value. When you click on thecheckbox
it evaluatesholiday= !holiday && employee= !employee
, bothholiday
&employee
value becomestrue
.Basically behind the scene when 1st
holiday= !holiday
get evaluated,holiday
becomestrue
tofalse
&holiday= !holiday
expression return latest value(returnstrue
), whether 2nd expression does the same thing & returnstrue
.Now
holiday = true
&employee = true
. When you click on the check box again. It calls change event & again try to evaluateholiday= !holiday && employee= !employee
. Whereholiday= !holiday
returnfalse
, so then as I mention above How&&
operator works?. It doesn't care about the next part of expression, and returnsfalse
value.Now
holiday = false
&employee = true
. If you click on checkbox again, thenholiday
turns out totrue
& proceed to evaluate other part of expression by whichemployee
turns out totrue
fromfalse
.这篇关于(改变)angular2事件挂接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!