如果为false,则保存到localstorage并切换并删除localstorage(save to localstorage is toggle on and delete localstorag

编程入门 行业动态 更新时间:2024-10-28 08:30:02
如果为false,则保存到localstorage并切换并删除localstorage(save to localstorage is toggle on and delete localstorage if its false)

我正在使用离子和angularjs的应用程序,这是我想要实现的,当切换开关打开时,它应该将localstorage设置为true,当它关闭时它应该删除localstorage。

.controller('shops',['$scope','$http','$timeout',function($scope,$http,$timeout) { $http.get('http://localhost/work/templates/source.php').success(function(data){ $scope.shops=data; }); $scope.pushNotificationChange = function(item) { $timeout(function() { if ($scope.pushNotification = { checked: true }) { alert("false"); } //alert('Push Notification Change: '+ $scope.pushNotification.checked); localStorage.setItem("shop_id",($scope.item.shop_id)); }, 0); }; }]);

HTML

<div align="right"> <label class="toggle toggle-balanced"> <input type="checkbox" ng-model="pushNotification.checked" ng-change="pushNotificationChange()"> <div class="track"> <div class="handle"></div> </div> </label> </div>

I'm working on an application with ionic and angularjs and this is what I want to achieve, when the toggle switch is on it should set the localstorage to true, when it's turned off it should delete the localstorage.

.controller('shops',['$scope','$http','$timeout',function($scope,$http,$timeout) { $http.get('http://localhost/work/templates/source.php').success(function(data){ $scope.shops=data; }); $scope.pushNotificationChange = function(item) { $timeout(function() { if ($scope.pushNotification = { checked: true }) { alert("false"); } //alert('Push Notification Change: '+ $scope.pushNotification.checked); localStorage.setItem("shop_id",($scope.item.shop_id)); }, 0); }; }]);

HTML

<div align="right"> <label class="toggle toggle-balanced"> <input type="checkbox" ng-model="pushNotification.checked" ng-change="pushNotificationChange()"> <div class="track"> <div class="handle"></div> </div> </label> </div>

最满意答案

编辑:更明确的答案

<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>Toggles</title> <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet"> <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script> </head> <body ng-controller="MainCtrl"> <ion-header-bar class="bar-positive"> <h1 class="title">Toggles</h1> </ion-header-bar> <ion-content> <ion-toggle ng-model="pushNotification.checked" ng-checked="pushNotification.checked"> {{ pushNotification.text }} </ion-toggle> <div class=""> {{pushNotification.checked}} </div> </ion-content> </body> </html>

你的js代码将是

angular.module('ionicApp', ['ionic']) .controller('MainCtrl', function($scope) { $scope.pushNotification = {}; $scope.pushNotification.text = "Sample" $scope.pushNotification.checked = false; $scope.pushNotificationChange = function() { console.log('Push Notification Change', $scope.pushNotification.checked); if($scope.pushNotification.checked){ //set your local storage }else{ //remove from local storage. } }; });

Edit: With more clear answer

<html ng-app="ionicApp"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>Toggles</title> <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet"> <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script> </head> <body ng-controller="MainCtrl"> <ion-header-bar class="bar-positive"> <h1 class="title">Toggles</h1> </ion-header-bar> <ion-content> <ion-toggle ng-model="pushNotification.checked" ng-checked="pushNotification.checked"> {{ pushNotification.text }} </ion-toggle> <div class=""> {{pushNotification.checked}} </div> </ion-content> </body> </html>

Your js code will be

angular.module('ionicApp', ['ionic']) .controller('MainCtrl', function($scope) { $scope.pushNotification = {}; $scope.pushNotification.text = "Sample" $scope.pushNotification.checked = false; $scope.pushNotificationChange = function() { console.log('Push Notification Change', $scope.pushNotification.checked); if($scope.pushNotification.checked){ //set your local storage }else{ //remove from local storage. } }; });

更多推荐

本文发布于:2023-04-28 07:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331374.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:localstorage   false   save   delete   toggle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!