Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening if cookie has changed

I was wondering if there was a "correct" way to listen for cookie changes on Angular? It would be awesome if I could listen and change values in my models depending on their new values.

Thanks.

like image 463
ddibiase Avatar asked Aug 15 '13 02:08

ddibiase


1 Answers

'use strict';

app.controller('MainCtrl', function ($scope, $log, $cookies, $timeout) {

    $scope.$watch(function() { return $cookies.test; }, function(newValue) {
        $log.log('Cookie string: ' + $cookies.test)
    });

    $cookies.test = 'first value';

    $timeout(function () {
        $cookies.test = 'second value';
    }, 1000);

});
like image 157
Dmitry Paloskin Avatar answered Oct 19 '22 17:10

Dmitry Paloskin