2016-09-29 20 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

希望我已經幫助你。 :)

相關問題