(更改)事件钩角度2 [英] (change) event hook in angular2

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

问题描述

我知道角色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?


  1. 假设 a&& b ,如果两者都为true,那么只有它返回true(简而言之,不应该有任何 false 值,否则返回false) / li>
  2. 评估 a&& b 代码将首先检查 a 值是 true 然后只有解释器将 b 值来评估它。如果 a 值,它的自身是
    false 然后它不评估(检查) b 的值,而 a&& b
    表达式将返回 false

  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.


在你有假期=!假期&&& 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= !holidayexpression 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屋!

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