如果为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. } }; });更多推荐
发布评论