2015-01-06 46 views
0

我无法在Angular.js中获取cookie。这是我使用的代码无法在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

是'$ username'在cookie中存在? –

+0

是的。我可用。 – Jayanth

+0

我想你应该使用(用户!== undefined) – murnax

回答

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); 
0
angular.module('Routes', ['ngRoute','reviewService', 'ngCookies']) 

$饼干是ngCookies模块,你需要进口。不要忘记在html中添加<script>标签。

+0

中添加了

  • 11. Laravel 5.2无法读取会话cookie
  • 12. 无法使用Angular读取cookie标头
  • 13. 无法读取angular.js控制器中的json响应属性
  • 14. 无法在UIWebview浏览器中使用document.cookie读取cookie
  • 15. 无法在本地主机上的php中读取cookie
  • 16. 无法在javascript中第一次读取cookie
  • 17. 在javascript中读取cookie
  • 18. 在C#中读取Cookie的
  • 19. 在c中读取cookie#
  • 20. 无法读取cookie(使用jQuery和cookie插件)
  • 21. 无法使用jQuery-cookie插件编写/读取cookie
  • 22. 的jQuery插件的cookie无法读取或写入cookie的
  • 23. 无法获取Cookie
  • 24. angular.js:12520 TypeError:无法读取未定义的属性'post'
  • 25. angular.js:13550 TypeError:无法读取未定义的属性'compile'?
  • 26. Angular.js:10671 TypeError:无法读取undefined的属性
  • 27. Angular.js $ http.post类型错误:无法读取未定义
  • 28. angular.js - TypeError:无法读取未定义的属性'then'
  • 29. angular.js:14110 TypeError:无法读取未定义的属性'推'
  • 30. 我得到的错误为angular.js:12520TypeError:无法读取属性'then'undefined ..