【问题标题】:how to use ion-toggle in notification using ionic2 and angular2如何使用 ionic2 和 angular2 在通知中使用 ion-toggle
【发布时间】:2017-02-07 09:05:24
【问题描述】:

这里我使用切换图标来设置通知活动/非活动。我正在从 get call 获得响应。 在 get 调用中,我得到的通知值是 0 或 1,但我使用的 ts 文件 noteValue 是布尔值,意味着我们传递的 true 是 false。

但是从数据库端我们得到 0 或 1,数据库端它们仅将变量作为布尔值传递,但数据库将值存储为 0 或 1。

由于 noteValue 切换图标工作不正确,这意味着如果我在刷新应用程序时传递 true 或 false,它(切换图标)只能显示 false。如果切换图标处于活动状态,它会传递错误值并且切换图标处于非活动状态它会通过真正的价值。

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");
          }

          });

  }



}

【问题讨论】:

    标签: angular typescript ionic2


    【解决方案1】:

    如果我理解正确,在 DB 中,您正在保存 0 和 1 之类的值,而 ion-toggle 需要 true 或 false,您必须根据这些值启用/禁用它。

    如果正确,使用 angular2 你可以这样做:

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

    它会根据your_DB_variable设置真或假。

    希望我对你有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-05
      • 2017-07-15
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2017-06-08
      • 2017-11-14
      相关资源
      最近更新 更多