【问题标题】:AngularJS cookie is not workingAngularJS cookie 不工作
【发布时间】:2016-01-15 05:53:42
【问题描述】:

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');

alert(favoriteCookie);

})

问题:

我是 Angular JS Ionic 的新手。我正在尝试在 angularjs 中使用 cookie

当我使用 $cookieStore

我得到以下异常

例外:

Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module ngCookies due to:
Error: [$injector:nomod] Module 'ngCookies' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

我将 ngCookies 添加到 ngular.module 中,但它对我不起作用。

我到底想念哪里?我如何在 Angular js 中使用 cookie。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: javascript angularjs cookies ionic


    【解决方案1】:

    我发现 $cookies 在 Cordova/Phonegap 应用程序中不起作用,因为这些页面是作为文件 URL 提供的。在普通的桌面浏览器中,如果您从文件系统提供页面,它也无法工作 - 通过服务器或 localhost 提供页面时它可以工作。

    所以我假设角度 $cookies 不适用于 Cordova 应用程序。 也许有一个解决方法?我只是不知道,也许现在有人回答了。

    【讨论】:

    • 我刚刚使用了 AngularJS localStorage,API 几乎相同,并且可以正常工作。
    • 我有同样的问题......它似乎不适用于 Cordova 应用程序(在我的情况下是 Ionic)。我会检查本地存储。谢谢哈巴。
    【解决方案2】:

    可能你错过了添加angular-cookies.js文件

    如果有,添加它。

    【讨论】:

    • 很高兴为您提供帮助 :),加油
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2016-07-23
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多