【问题标题】:Angular $cookieStore do not set keyAngular $cookieStore 不设置密钥
【发布时间】:2015-11-08 01:27:03
【问题描述】:

应用内

 var appRoot = angular.module('AppRoot', ['AppRoot.Factory', 'AppRoot.Constants', 'ngResource', 'ui.router']);
 var factory = angular.module('AppRoot.Factory', ['ngCookies', 'ngDialog']);
 var constants = angular.module('AppRoot.Constants', []);

饼干工厂

///Cookie store service
(function () {
    'use strict';

    angular.module('AppRoot.Factory').factory('CookieFactory', ['$cookieStore',
    function ($cookieStore) {
        return {
            ///set value on key
            set: function (key, value) {
                if ('undefined' === typeof value)
                    value = '';

                $cookieStore.put(key, value);
            },
            ///return null or value of key
            get: function (key) {
                var value = $cookieStore.get(key);
                if ('undefined' === typeof value)
                    return null;

                    return value;
            },
            ///test if key exist in cookie store
            keyExist: function (key) {
                return 'undefined' === typeof ($cookieStore.get(key));
            }
        }
    }]);
})();

我尝试这样使用

(function () {
    'use strict';
    angular.module('AppRoot')
        .controller('RootController', ['$rootScope','CookieFactory',
            function ($rootScope,CookieFactory) {  
                CookieFactory.set('test', 'test');    
                var test = CookieFactory.get('test');     
            }]);
})();

"var test" 为空,如果我尝试调用 CookieFactory.keyExist('test') 返回 false

我使用 AngularJS v1.4.3。

  1. Cookies set by this page
  2. Resources

    我做错了什么?

编辑:

///Cookie store service
    (function () {
        'use strict';

        angular.module('AppRoot.Factory').factory('CookieFactory', ['$cookies',
        function ($cookies) {
            return {
                ///set value on key
                set: function (key, value) {
                    if ('undefined' === typeof value)
                        value = '';

                    $cookies.put(key, value);
                },
                ///return null or value of key
                get: function (key) {
                    var value = $cookies.get(key);
                    if ('undefined' === typeof value)
                        return null;

                        return value;
                },
                ///test if key exist in cookie store
                keyExist: function (key) {
                    return 'undefined' === typeof ($cookies.get(key));
                }
            }
        }]);
    })();

【问题讨论】:

  • 一般来说,cookieService 已经贬值了。你应该使用 $cookies。
  • 与“$cookies”的行为相同

标签: angularjs


【解决方案1】:

您使用 AngularJS v 1.4.3。 $cookiesService 已在 1.3 版中贬值。使用 $cookies。

以下是如何使用它的示例:

    angular.module('cookiesExample', ['ngCookies'])
   .controller('ExampleController', ['$cookies', function($cookies) {
  // Retrieving a cookie
  var favoriteCookie = $cookies.get('myFavorite');
  // Setting a cookie
  $cookies.put('myFavorite', 'oatmeal');
}]);

【讨论】:

  • 与“$cookies”的行为相同
猜你喜欢
  • 2015-08-26
  • 1970-01-01
  • 2013-08-07
  • 2020-03-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多