Angular $ cookieStore不设置密钥

时间:2015-11-07 16:06:43

标签: angularjs

在应用

 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工厂

///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

    我做错了什么?

  3. 编辑:

    ///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));
                    }
                }
            }]);
        })();
    

1 个答案:

答案 0 :(得分: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');
}]);