Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

What's the AngularJS way to access cookies? I've seen references to both a service and a module for cookies, but no examples.

Is there, or is there not an AngularJS canonical approach?

1 Answer

0 votes
by (106k points)
edited by

If you want to access cookies in AngularJS then you can set and get cookie values. 

Are you interested in learning Angularjs from the basics! Here's the right video for you on Angularjs provided by Intellipaat:

Note you should use $cookieStore instead of $cookies.

<!DOCTYPE html> 

<html ng-app="myApp"> 

<head> 

<script src="http://code.angularjs.org/1.0.0rc10/angular-1.0.0rc10.js">

</script> 

<script src="http://code.angularjs.org/1.0.0rc10/angular-cookies-1.0.0rc10.js"></script>

<script> 

angular.module('myApp', ['ngCookies']); 

function CookieCtrl($scope, $cookieStore) { 

$scope.lastVal = $cookieStore.get('tab'); 

$scope.changeTab = function(tabName){ 

$scope.lastVal = tabName; 

$cookieStore.put('tab', tabName); 

}; 

</script> 

</head>

<body ng-controller="CookieCtrl"> </body> 

</html>

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...