CleanCrispCode CleanCrispCode - 6 months ago 40
AngularJS Question

JavaScript Cookie VS angularjs $cookies

whats the difference between a cookie created using JavaScript and the one created using angularjs $cookies.

If I create a cookie using JavaScript syntax like mentioned below , cookie values are retained even when user refreshes the browser.

document.cookie = "username=smith";


Whereas when I use $cookies , cookie values are not retained when user refreshes the browser.

$cookies.FromPage = 'has value';


I was expecting that the values to be retained in $cookies also.

Answer

When using $cookies in angular > 1.3 you should use its methods put(key, primitiveValue), putObject(key, objectValue) and remove(key) to update the store and get(key) or getAll() to access its content.