I'me a noob on AngularJS. I'm trying to manage cookies but it doesn't works as I want. I've two version of this code that seams identical but if the first one works... The second one failed.
The code for my login form is here :
(function() {
var app = angular.module('userApp', ['suiviChantiers', 'ngCookies']);
app.controller('UserLogin', ['$scope', '$http', '$cookies',
function($scope, $http, $cookies) {
$scope.login = function() {
$scope.user = this.user;
$http.post('services/user.php', {
action: 'login',
user: $scope.user
}).success(function(data) {
if (data.error) {
// [...]
} else if (data.message == 'success') {
$cookies.user = data.data;
console.log("User : " + $cookies.user); // Output is : User : MS1OVFEwWkRZeU5ETTBNVFV3TVE9PQ==
window.setTimeout(function() {
if ($cookies.redirectTo == null) {
document.location.href = "chantier/index.html";
} else {
var dest = $cookies.redirectTo;
$cookies.redirectTo = null;
document.location.href = dest;
}
}, 3000);
}
});
};
// [...]
})();
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
<h1>Login form ...</h1>
And in my "chantier/index.html" controller I have this :
(function() {
var app = angular.module('chantierApp', ['ngCookies']);
app.controller('ChantierListController', ['$scope', '$cookies', 'userService', function($scope, $cookies, userService) {
$scope.user = userService.user;
console.log("User : "+$cookies.user); // Output is : User : undefined but $cookies.redirectTo is not empty
}]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-cookies.min.js"></script>
(Sorry for this so long code post) Can you explain me why cookies are not updated?
Thanks for help.