The ngCookies module provides a convenient wrapper for reading and writing browser cookies. By using cookieStore and cookie, we can read and write cookies. Add angular and angular-cookie script to your page.
<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>

Using $cookieStore

    angular.module('myApp', ['ngCookies']);
    function CookieCtrl($scope, $cookieStore) {
      
      $scope.setCookie = function(){
          $cookieStore.put('name', 'srinivas');
          alert('cookie was set');
      };

      $scope.readCookie = function(){
          alert($cookieStore.get('name'));
      };
      
      $scope.removeCookie = function(){
          $cookieStore.remove('name');
          alert('cookie was removed');
      }; 
    }

Using $cookies

angular.module('myApp', ['ngCookies']);
    function CookieCtrl($scope, $cookies) {
      
      $scope.setCookie = function(){
          $cookies.name = 'srinivas';
          alert('cookie was set');
      };

      $scope.readCookie = function(){
          alert($cookies.name);
      };
      
      $scope.removeCookie = function(){
          $cookies.name = null;
          alert('cookie was removed');
      };    
    }

0 comments:

Blogroll

Popular Posts