繁体   English   中英

角度$ cookieStore在控制器$ scope函数下不起作用

[英]angular $cookieStore not working under controller $scope functions

我在使用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

我现在遇到了这个问题。 您的帖子向我指出了正确的问题。 我创建了一个plnkr尝试进行复制,并注意到我能够在$ scope方法内很好地执行$ cookieStore.put()。 因此,问题似乎在于验证源文件。 我为angular.js和angular-cookies.js使用了1.3.11,但为angular-route.js使用了1.3.5。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM