每次加载单页应用程序都在检索cookie

Retrieving cookie with every load of Single Page App

本文关键字:检索 cookie 应用程序 加载 单页      更新时间:2023-09-26

需要对下面的AngularJS代码进行哪些具体更改,以在每次加载页面时检查是否存在名为myCookie的cookie,然后将$rootScope.myCookieValue变量设置为myCookie的值

代码来自示例应用程序,您可以在此链接中查看其完整代码。这是一个非常简单的示例应用程序,我只想要一个简单的工作解决方案,这样我就可以从中构建更复杂的方法

angular.module('hello', [ 'ngRoute' ]).config(function($routeProvider, $httpProvider) {
    $routeProvider.when('/', {
        templateUrl : 'home.html',
        controller : 'home',
        controllerAs : 'controller'
    }).otherwise('/');
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.common['Accept'] = 'application/json';
}).controller('navigation',
function($rootScope, $http, $location, $route) {
    var self = this;
    self.tab = function(route) {
        return $route.current && route === $route.current.controller;
    };
    $http.get('user').then(function(response) {
        if (response.data.name) {
            $rootScope.authenticated = true;
        } else {
            $rootScope.authenticated = false;
        }
    }, function() {
        $rootScope.authenticated = false;
    });
    self.credentials = {};
    self.logout = function() {
        $http.post('logout', {}).finally(function() {
            $rootScope.authenticated = false;
            $location.path("/");
        });
    }
}).controller('home', function($http) {
    var self = this;
    $http.get('resource/').then(function(response) {
        self.greeting = response.data;
    })
});

我想你可以在运行块中完成:

 angular.module('hello', ['ngCookies', 'ngRoute'])
.config(function ($routeProvider, $httpProvider) {
    $routeProvider.when('/', {
        templateUrl: 'home.html',
        controller: 'home',
        controllerAs: 'controller'
    }).otherwise('/');
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $httpProvider.defaults.headers.common['Accept'] = 'application/json';
}).run(function ($rootScope, $cookies) {
    var cookieValue = $cookies.get("myCookie");
    if (cookieValue) {
        $rootScope.myCookieVar = cookieValue;
    }
}).controller('navigation',
function ($rootScope, $http, $location, $route) {
    var self = this;
    self.tab = function (route) {
        return $route.current && route === $route.current.controller;
    };
    $http.get('user').then(function (response) {
        if (response.data.name) {
            $rootScope.authenticated = true;
        } else {
            $rootScope.authenticated = false;
        }
    }, function () {
        $rootScope.authenticated = false;
    });
    self.credentials = {};
    self.logout = function () {
        $http.post('logout', {}).finally(function () {
            $rootScope.authenticated = false;
            $location.path("/");
        });
    }
}).controller('home', function ($http) {
    var self = this;
    $http.get('resource/').then(function (response) {
        self.greeting = response.data;
    })
});

我认为你应该使用角度版本>1.4.x

您还应该添加参考angular-cookies.js