CleanCrispCode CleanCrispCode - 1 year ago 138
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download