javascript - 简化逻辑
问题描述
我是 Angular 的新手,正在学习一些在线课程,我正在尝试简化以下逻辑,例如使用三元运算符等。
onClick() {
if (this.demo === true) {
this.test = true;
} else {
this.test = false;
}
}
解决方案
您可以直接分配检查结果。
this.test = this.demo === true; // strict check for type and value
如果this.demo
是一个布尔值,那么只分配这个值就足够了。
this.test = this.demo;
推荐阅读
- java - 如何从源包而不是资源目录加载文件
- spring - 使用 Spring Boot 托管单页应用程序
- java - 如何从 Google Contacts API 获取备注字段
- javascript - 浏览器插件或扩展程序能否查看页面中运行的 javascript 变量的值
- windows - windows IOCTL 命令支持的最大超时是多少?
- json - 如何管理空选择结果
- elasticsearch - Elasticsearch 映射似乎不适用于查询
- javascript - 选中时使用多个复选框显示/隐藏多个 Div
- excel - 电子邮件可以通过 VBA 发送,但当我使用属性 .SentOnBehalfOfName 时仍留在发件箱中
- extjs - extjs反应器有CDN链接吗