angular - 如何使用 ionic2 和 angular2 在通知中使用 ion-toggle

标签 angular typescript ionic2

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

但是从数据库端我们得到的不是 0 就是 1,数据库端他们只将变量作为 bool 值传递,但数据库存储的值是 0 或 1。

因为 noteValue 切换图标工作不正确,意味着如果我在刷新应用程序时传递 true 或 false,它(切换图标)只能显示 false。如果切换图标处于事件状态,它会传递 false 值,而切换图标处于非事件状态时,它会通过真正的值(value)。

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

          });

  }



}

最佳答案

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

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

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

它会根据 your_DB_variable 设置 true 或 false。

希望对你有所帮助。 :)

关于angular - 如何使用 ionic2 和 angular2 在通知中使用 ion-toggle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39763513/

相关文章:

angular - Ionic 4 和带有网络电容器的 toastr 无法离线使用 PWA 应用程序

angular - 在 ionic 3 中创建自定义对话框/弹出窗口/警报

javascript - 在 TypeScript 中使用第三方 js 文件

ios - 首次加载时闪屏挂起的 IONIC 白屏问题(iOS - ionic3)

angular - 为什么我无法获得体形 Angular

angular - 使用 TypeScript 销毁静态变量、静态数组

javascript - 如何将 tsd 定义自动链接到 bower 和 npm?

javascript - typescript 类型 : array of T to map

javascript - JS从字符串中提取数据

angular - 在构造函数中使用 T 创建通用组件