与AngularJS的Cookie

时间:2017-07-31 10:50:06

标签: angularjs cookies

我正在尝试在我的应用程序中应用cookie,我在阅读许多教程时做了,但我没有得到任何结果,也没有显示错误。我想在cookie中保存经过身份验证的用户并在其他视图中显示它!请问哪里有问题!

app.js

app.controller('loginCtrl', function($scope, $location,$state,$http,$window,$rootScope,$cookies){

    $scope.submit = function()
    {
        data = {
            'Email' : $scope.Email,
            'mdp' : $scope.mdp
    };

        $http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config,result)
        {
            console.log(data);

            if(data.role == 'client'){
                $state.go('view');  
                console.log(data);
                 $scope.myCookie=$cookies.get('cookie');
                $scope.setCookie = function(data){
                  $cookies.put('cookie',data);
                }


             }

view.html

<div class="container" ng-controller="loginCtrl">
{{myCookie}}

1 个答案:

答案 0 :(得分:0)

尝试 ngCookies 先安装

      scrollView.delegate = self
        for localImage in DemoConstants.localImages {


        if let image = UIImage(named: localImage.fileName) {
            scrollView.auk.show(image: image)
      }}





    let pageIndex = scrollView.auk.currentPageIndex
    print(pageIndex!)



    scrollView.auk.settings.contentMode = UIViewContentMode.scaleAspectFill
    scrollView.auk.settings.pagingEnabled = true
    self.scrollView.auk.settings.pageControl.backgroundColor = UIColor.black
    scrollView.auk.startAutoScroll(delaySeconds: 3)
    scrollView.auk.scrollToPage(atIndex: 2, animated: true)

在index.html中添加路径

npm install --save angular-cookies

最后,通过将模块添加为依赖模块来加载应用程序中的模块:

<script src="path/to/angular-cookies.js"></script>

然后按照以下方式编辑您的控制器

var app = angular.module('app', ['ngCookies']);

以及何时想要访问Cookie

app.controller('loginCtrl', function($scope, $location,$state,$http,$window,$rootScope,$cookieStore){

$scope.submit = function()
{
    data = {
        'Email' : $scope.Email,
        'mdp' : $scope.mdp
};

    $http.post('http://localhost/deb/login.php', data)
    .then(function(res)
    {


        console.log(res.data);

        if(res.data.role == 'client'){
            $state.go('view');  
            $cookieStore.put('email',res.data.email);
            $cookieStore.put('token',res.data.token);
         }

了解更多信息,请查看doc

修改

现在以不同的方式显示所有内容

$cookieStore.get('cookieName');