Aktywne Wpisy
źródło: 1000016630
Pobierz
Piotrek7231 +54
Zdrowie Polskich pięknych klubów wasze i moje bo mam urodziny. #mecz
źródło: temp_file2401767779157533066
PobierzSkopiuj link
Skopiuj link
źródło: 1000016630
Pobierz
źródło: temp_file2401767779157533066
PobierzRegulamin
Reklama
Kontakt
O nas
FAQ
Osiągnięcia
Ranking
.service('Auth',['$http','TokenStorage','$rootScope', function($http, TokenStorage, $rootScope) {
var authenticated = false;
var username = {};
this.getAuthenticated = function () {
return authenticated;
},
this.init = function () {
$http.get('http://localhost:8080/api/users/current').success(function (user) {
if(user.username !== 'anonymousUser'){
authenticated = true;
username = user.username;
}
});
};
this.login = function (username, password) {
$http.post('http://localhost:8080/api/login', { username: username, password: password }).success(function (result, status, headers) {
authenticated = true;
TokenStorage.store(headers('X-AUTH-TOKEN'));
});
};
this.logout = function () {
TokenStorage.clear();
authenticated = false;
};
}])
i chce zeby zmienna authenticated aktualizaowała sie w całym projekcie.
Wiem, że można to zrobic przy pomocy watcha, ale jak to zrobic przy pomocy broadcast ;d Co wpisac w serwisie($broadcast tylko jak) i co wpisac w glownym kontrolerze app($on tylko jak):
angular.module('app')
.controller('AppCtrl',[ 'Auth' ,function (Auth) {
var app = this;
$scope.authenticated = Auth.getAuthenticated();
}]);
#angularjs
authenticated = true;, powinieneś wywołać$rootScope.$broadcast('auth.loggedin')(nazwa tutaj jest dowolna, ważne, by była wszędzie jednolita). W głównym kontrolerze natomiast$rootScope.$on('auth.loggedin', function() { /* użytkownik został zalogowany */ }). To najprostsze rozwiązanie..service('Auth',['$http','TokenStorage','$rootScope', function($http, TokenStorage, $rootScope) {var authenticated = false;
$rootScope.$broadcast('auth.loggedout');
var username
$scope.authenticated = Auth.getAuthenticated();Zapomniałeś o nawiasach.