【问题标题】:AngularJS, $cookieStore.put is not a functionAngularJS,$cookieStore.put 不是一个函数
【发布时间】:2015-09-18 01:28:35
【问题描述】:

我收到错误 - “当我尝试在我的应用程序中保存 cookie 时,$cookieStore.put 不是一个函数。我使用的是 AngularJS 1.4.1 和 cookie 相同的版本。

var examinationApp = angular.module("examinationApp", ['ngCookies']);
examinationApp.controller("examinationCtrl", ['$scope', '$window', '$http',
'$interval','$cookieStore', function ($scope, $window, $http, $cookieStore,
modalDialog){
$scope.saveTestsToCookieStore = function () {
            $cookieStore.put('answeredTests',JSON.stringify($scope.data));
            $cookieStore.put('answeredQuestions',JSON.stringify($scope.answeredTests));
            $cookieStore.put('questionStopped',$scope.currentQN);
        }...

当我尝试保存 cookie 时出现错误 - TypeError: $cookieStore.put is not a function。

我做错了什么?

【问题讨论】:

    标签: angularjs cookiestore


    【解决方案1】:

    您的参数没有对齐...

    将内联数组注释中的依赖项列表与函数声明中的参数列表进行比较。依赖项列表必须对齐(相同的依赖项以相同的顺序)。 (基本上,您的控制器函数试图在 $interval 而不是 $cookieStore 上调用 put

    你有什么...

    examinationApp.controller("examinationCtrl", [
      '$scope', '$window', '$http', '$interval','$cookieStore', 
      function ($scope, $window, $http, $cookieStore, modalDialog) {
    

    你需要什么...

    examinationApp.controller("examinationCtrl", [
      '$scope', '$window', '$http', '$cookieStore', 'modalDialog', 
      function ($scope, $window, $http, $cookieStore, modalDialog) {
    

    【讨论】:

      【解决方案2】:

      Angularjs >= 1.3

      $cookieStore 服务已弃用。请使用 $cookies 代替服务。

      您应该使用$cookies 来读取/写入浏览器的cookies。

      【讨论】:

        【解决方案3】:

        Angular 版本 == 1.3.5 ,假设验证后在 Application Security 类中设置了标头值“X-AUTH-TOKEN = 'eyJwYXNzd29yZCI6ImFkbWlu'”。

        $scope.postData = "{\"username\" : username , \"password\": password ,\"email\" :email}";
        
        $http({
                    method: 'POST',
                    url: '/API/authenticate',
                    data: postData,
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded",
                        "X-Login-Ajax-call": 'true'
                    }
                })
                .then(function(response) {
                    if (response.data == 'ok') {
                        $cookies['X-AUTH-TOKEN']=response.headers('X-AUTH-TOKEN');
                        window.location.replace('/');
                    }
                    else {
        
                        // Error Message...
                    }
                });
        

        【讨论】:

          猜你喜欢
          • 2014-08-08
          • 2015-09-12
          • 2016-05-21
          • 2018-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-08
          • 2016-07-12
          相关资源
          最近更新 更多