如何更新if条件中的值

时间:2016-09-21 11:07:41

标签: if-statement angular typescript ionic2

我的情况是checknotificationObj="true"(即_this.noteValue="ture"),如下所示。

现在如何在.ts中输入if条件时_this.noteValue =false。这个相同的visiversa for else part

我是angular2的新手,请帮助我

let checkNotifictionObj = _this.noteValue;
     if(checkNotifictionObj){
        notificationDetails={
          notification:"flase"
        };
       _this.noteValue="false";
     }
     else{
       notificationDetails={
         notification:"true"
       };
       _this.noteValue="true";
     }
  

我无法将checkNotifictionObj true更新为falsefalse更新为true

2 个答案:

答案 0 :(得分:0)

我想你想这样做:

let checkNotifictionObj:boolean = _this.noteValue; // _this.noteValue must be of boolean type
if(checkNotifictionObj){
  notificationDetails={
    notification : false
  };
  _this.noteValue = false;
} else {
  notificationDetails={
    notification : true
  };
  _this.noteValue = true;
}

答案 1 :(得分:0)

我想你想做的是:

  1. 如果_this.noteValuetrue

    • 设置

      notificationDetails = {
        notification : false
      }
      
    • _this.noteValue设为false
  2. 如果_this.noteValuefalse

    • 设置

      notificationDetails = {
        notification : true
      }
      
    • _this.noteValue设为true
  3. 更简单的方法就是:

    _this.noteValue = !_this.noteValue; // If _this.noteValue is equal to true, now it will be set to false. If it's false, it will be set to true.
    
    notificationDetails = {
      notification: _this.noteValue
    };
    

    这样你就不需要if/else