我无法在Angular.js中获取Cookie。我使用的代码是
angular.module('Routes', ['ngRoute','reviewService']).config(['$routeProvider','$locationProvider','$cookies','$cookieStore',
function($routeProvider, $locationProvider,$cookies,$cookieStore) {
var user=$cookies['username']
if(user !='undefined')
{
$routeProvider
.when('/', {
templateUrl: '/view/home.html',
controller: 'myController'
})
............
我在浏览器控制台中收到的错误是
Error: [$injector:modulerr] Failed to instantiate module Routes due to:
Error: [$injector:unpr] Unknown provider: $cookies
........
答案 0 :(得分:1)
最后这段代码工作正常..
angular.module('Routes',['ngRoute','reviewService','ngCookies']).config(['$routeProvider','$locationProvider',
function($routeProvider, $locationProvider) {
var $cookies;
angular.injector(['ngCookies']).invoke(function(_$cookies_) {
$cookies = _$cookies_;
});
var user=$cookies['username']
console.log("user name:",user);
答案 1 :(得分:0)
angular.module('Routes', ['ngRoute','reviewService', 'ngCookies'])
$ cookies在ngCookies模块中,您需要导入。别忘了在html中添加<script>
标记。