2016-09-29 23 views
2

在这里,我使用切换图标来设置通知活动/非活动状态,我从获取调用中获得响应。 在获取调用中,我得到的通知值为0或1,但是我使用的noteValue的ts文件是布尔值,意味着我们传递的值为false。如何在使用ionic2和angular2的通知中使用离子切换

但是从数据库侧我们得到0或1,数据库侧他们只能传递变量作为布尔值,但数据库存储的值作为0或1

由于noteValue切换图标的工作不正确的,也就是说,如果我通过当我刷新应用程序时,它是正确的还是错误(切换图标)只能显示假。如果切换图标处于活动状态,则会传递错误值并且切换图标处于非活动状态,并传递真实值。

HTML:

<ion-item no-lines > 
    <ion-label class="ion-label"> Notification</ion-label> 
    <ion-toggle (ionChange)="updateValue()" checked="noteValue"></ion-toggle></ion-item> 

.TS:

export class NotificationPage { 
public noteValue:boolean; 
constructor(private navCtrl: NavController,private user:Users, private productServices:Products, private logger:Logger) { 
    var _this = this; 

// get call 
     this.productServices. getNotification(function(data){ 
     _this.logger.debug("checking my notification Details" +JSON.stringify(data)); 
     console.log(data.notification); 
     _this.noteValue = data.notification[0]; 
     console.log(data.notification[0]); 

     }) 

    } 

//post call 

updateValue(){ 
    var _this=this; 

    _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. 

    let notificationDetails = { 
     notification: _this.noteValue 
    }; 
     console.log(notificationDetails); 
     this.user.setNotification(notificationDetails, function (result, data) { 
     _this.logger.debug("checking my notification Details" +data); 
      if (result=='1') { 
      console.log(_this.noteValue); 
      //_this.noteValue=!_this.noteValue; 
      _this.logger.debug("checking my notification Details" +data); 
      alert("sucesscall for notification"+data.toString()); 
      } 
      else { 
      _this.logger.info("failure Notification"); 
      alert("failure for notification"); 
      } 

      }); 

    } 



} 

回答

0

如果我理解正确的话,在DB您要保存如0和1的值,而ion-toggle希望真的还是假的,你有根据这些值启用/禁用它。

如果正确的话,与angular2你可以做这样的事情:

<ion-toggle (ionChange)="updateValue()" checked="{{your_DB_variable === 1 ? 'true':'false'}}"></ion-toggle> 

它将设置true或false取决于your_DB_variable

希望我已经帮助你。 :)

相关问题