Ionic Angular JS cookie无效

时间:2015-10-16 14:07:45

标签: javascript android angularjs cookies ionic

app.js

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services', 'ngCordova', 'ngCookies'])

.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {

if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);

}
         if (window.StatusBar) {
                StatusBar.styleLightContent();
         }

});
})

.config(function ($stateProvider, $urlRouterProvider) {

$stateProvider

  .state('HomePage', {
             url: '/HomePage/:id',
             templateUrl: 'templates/HomePage.html',
             controller: 'HomePageCtrl'
         })
         .state('login', {
             url: '/login',
             templateUrl: 'templates/Login.html',
             controller: 'LoginCtrl'
         });
  $urlRouterProvider.otherwise('/login');

});

controllers.js

angular.module('starter.controllers', [])

.controller('LoginCtrl', function ($scope, LoginService, $ionicPopup, $state, $location, $http, $cookieStore) {

$cookieStore.put('mycookie', 'cookie value');
var favoriteCookie = $cookieStore.get('mycookie');

   var alertPopup = $ionicPopup.alert({
   title: 'favoriteCookie title',
   template: favoriteCookie // This does not work on phone.

});})

的index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">


    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="lib/ionic/ng-cordova-master/demo/www/lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
    <script src="lib/ionic/js/cookies.js"></script>

</head>
<body ng-app="starter">

    <ion-nav-view></ion-nav-view>

</body>
</html>

问题:

如果我使用$cookieStore,它可以在浏览器上正常运行。

但是,当我将项目发布到手机$cookieStore时无效。我的意思是当我尝试在Android手机中使用$cookieStore时。

$cookieStore$ionicPopup.alert上显示以下错误。

undefined

如何使用离子?在/ android上使用cookie?

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:2)

不要使用$ cookie似乎在Android上运行得很好,只需将其替换为默认的localStorage即可。它工作得很好! :)

$cookieStore.set() -> window.localStorage.setItem() $cookieStore.get() -> window.localStorage.getItem() $cookieStore.remove() -> window.localStorage.removeItem()

答案 1 :(得分:1)

请将此答案投票,因为当我发布&gt; _&lt;时,我没有正确登录到另一个答案。卫生署!

不要使用$ cookie似乎在Android上运行得很好,只需将其替换为默认的localStorage即可。它工作得很好! :)

$cookieStore.set() -> window.localStorage.setItem() $cookieStore.get() -> window.localStorage.getItem() $cookieStore.remove() -> window.localStorage.removeItem()