javascriptangularjscookiesangular-cookies

In Angular 1.2.32 How can I set expiry to cookie?


I am using angular 1.2.32 in an old project and I need to know how can I set the expiry of cookie. I am using ngCookies for it and trying to set cookie using below line:

 $cookies.putObject("newotpcookieobject", '7668787', { 'expires': expiresValue });

here the expiresValue is datetime object in javascript, code for getting it is below:

var today = new Date();
            var expiresValue = new Date(today);
expiresValue.setMinutes(today.getMinutes() + 120);

I am new to angularJS framework.


Solution

  • As discussed in the comments, your issue lies in the version of ngCookies. The possibility to set the expiration date on a cookie is only allowed on 1.4 or greater.

    Since your AngularJS version is 1.2.32, you should bump the version of ngCookies and AngularJS to, at least, 1.4.0.

    If you can't change the versions, you could default to the native cookies API. More informations on the native cookies API can be found in this question