首页 > 解决方案 > 简化逻辑

问题描述

我是 Angular 的新手,正在学习一些在线课程,我正在尝试简化以下逻辑,例如使用三元运算符等。

onClick() {
    if (this.demo === true) {
      this.test = true;
    } else {
      this.test = false;
    }
  }

标签: javascript

解决方案


您可以直接分配检查结果。

this.test = this.demo === true; // strict check for type and value  

如果this.demo是一个布尔值,那么只分配这个值就足够了。

this.test = this.demo;

推荐阅读