javascript - Angular JS - set $cookies domain wide failed -
i've been having troubles setting cookies in angularjs app. situation want set cookies available site-wide, have no idea how set params cookies using angular js default $cookies object.
for example, in javascript write this
var exp = new date(); exp.settime(exp.gettime()+(24*60*60*1000)); // expires after day document.cookie = "mycookies=yes;expires="+exp.togmtstring()+ ";domain=.example.com;path=/";
but dom object can't loaded app easily, have use $cookies (angular-cookies.js). new code is:
angular.module('myapp') .controller('myctrl', function ($scope, $filter, slug,public_routes, $cookies) { var mycookies = $cookies['mycookies']; if (typeof mycookies == 'undefined' || typeof mycookies == undefined) { $cookies['mycookies'] = "yes"; } });
but there's no way can set expiry date, path , domain not available $cookies.
what should do?
if you're using angular 1.4+ set $cookiesprovider.defaults
on config section of main app module. example:
angular.module('my-app', [ngcookies]) .config(['$cookiesprovider', function($cookiesprovider) { // set $cookies defaults $cookiesprovider.defaults.path = '/'; $cookiesprovider.defaults.secure = true; $cookiesprovider.defaults.expires = exp_date; $cookiesprovider.defaults.domain = my_domain; }]);
just don't make same mistake did @ first assigning defaults new object this:
$cookiesprovider.defaults = {path: '/', secure: true};
this break object's reference , defaults wouldn't overriden anymore.
Comments
Post a Comment