(更改)事件钩角度2 [英] (change) event hook in angular2
问题描述
我知道角色2中的(更改)事件绑定
,但是我为什么我的代码无法按预期工作而感到惊讶?
I know about the (change) event Binding
in angular2 but i am surprised why my code is not working as expected?
我的代码在这里..
http://plnkr.co/edit/9pSWSeqBc5oaSAtsfwNY?p=preview
当调用更改事件时,两个条件都不会按预期工作。
When the change event is called not both condition work as expected.
(change)=holiday =!holiday&& employee =!employee
当第一次调用更改事件时,第二次仅在第一个条件即假期
时才会工作。在我的例子中,我预期的是值是真或假,但不如预期。
When change event is called the first time it works fine but the second time it will work only for first condition i.e holiday
. In my example what I expected is both the value to be either true or false but not as expected.
当然有一些错误。有没有人可以正确解释(更改)
事件的生命周期?
Surely there is some mistake. Is there anyone who can explain the life cycle of the (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"
strong>如何
&&
运算符工作?
How
&&
operator works?
- 假设
a&& b
,如果两者都为true,那么只有它返回true(简而言之,不应该有任何false
值,否则返回false) / li>
- 评估
a&& b
代码将首先检查a
值是true
然后只有解释器将b
值来评估它。如果a
值,它的自身是
false
然后它不评估(检查)b
的值,而a&& b
表达式将返回false
。
- 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
.
在你有假期=!假期&&& employee =!employee
。在初始加载时,假期
& employee
有 false
值。当您点击复选框
时,它会评估 holiday =!holiday&& employee =!employee
, holiday
& 雇员
值变为 true
。
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
.
获得评估的第一个假期=!假期
成为
true
到 false
& holiday =!holiday
表达式返回最新值(返回 true
),第二个表达式是否同样的事情&返回 true
。
Basically behind the scene when 1st holiday= !holiday
get evaluated, holiday
becomes true
to false
& holiday= !holiday
expression return latest value(returns true
), whether 2nd expression does the same thing & returns true
.
现在 holiday = true
& employee = true
。当您再次单击复选框时。它叫变更事件&再次尝试评估 holiday =!holiday&&& employee =!employee
。
其中 holiday =!holiday
return false
,所以我上面提到如何&&
operator works?。它不关心表达式的下一部分,并返回 false
值。
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.
现在 holiday = false
& employee = true
。如果您再次点击复选框,则假期
变为 true
&继续评估 employee
的其他部分,从 false
true code>。
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
.
这篇关于(更改)事件钩角度2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!