【问题标题】:angular $cookieStore not working under controller $scope functions角度 $cookieStore 在控制器 $scope 函数下不起作用
【发布时间】:2014-12-14 10:14:24
【问题描述】:

我在使用 angular-js 时遇到问题

$locationProvider.html5Mode(true).hashPrefix('!');

一切都可以正常使用路由,预计 $cookieStore 不能在控制器 $scope 中定义的功能下工作,可能是因为我添加了 .HTACCESS。

我找不到任何解决方案,现在使用“window.localStorage”这里的任何人都有解决方案

我的示例代码如下:

HTML

 <button type="button" class="btn btn-default" ng-click='doLogin()'>Sign in</button>

应用脚本

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

 app.config(function($routeProvider, $locationProvider) {

    $routeProvider 
    .when('/dashboard', {
    templateUrl: 'pages/dashboard.html',
    controller: 'dashboardApp'
    })  
    .otherwise({
    redirectTo: '/',
    templateUrl: 'pages/login.html',
    controller: 'loginApp'
    });  
    $locationProvider.html5Mode(true).hashPrefix('!'); 
})


app.controller('loginApp', function($scope , $cookieStore ) {

    //This working fine
    $cookieStore.put('Greet','Welcome to Angular!');

    ///////////////////////////////////
    $scope.doLogin = function()
    { 
           // Here no cookie has been setting up
           $cookieStore.put('loggedin', true);
    };

})

.HTACCESS

#BEGIN
Options +FollowSymLinks

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^ index.html [L]
</ifModule>

#END

【问题讨论】:

    标签: javascript angularjs .htaccess angular-cookies


    【解决方案1】:

    我现在遇到了这个问题。你的帖子给我指出了正确的问题。 我创建了一个 plnkr 来尝试复制并注意到我能够在 $scope 方法中很好地执行 $cookieStore.put() 。 所以问题似乎与源文件的verisoning有关。我对 angular.js 和 angular-cookies.js 使用 1.3.11,但对 angular-route.js 使用 1.3.5。

    解决方法是为 angular-route.js 或您正在使用的相应版本使用 1.3.11。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2015-06-02
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 2015-08-31
      • 2017-11-10
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多