我在这里使用切换图标来设置通知事件/非事件。我正在从 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/