angularjs - Angular.js service return $http success data -
in app, want create service looks in backend check if user logged in , returns user data , privileges. factory service looks this:
app.factory 'session', ['$http', ($http) -> getsession: -> $http.get("/admin/session") ]
right now, handle success , errors service in controller shown below.
$scope.session = session.getsession() .success((data) -> # set logged in user $scope.admin = data # limit privileges ) .error((data) -> # logged in user not found rediret login screen window.location = "/user/login" )
my problem need repeat same code in lot of different controllers. there better way handle this? thinking might need $rootscope not understand how works.
you can put logic inside service itself. have use promises returning success data controller. controller, this:
session.getsession().then(function(data){ $scope.admin = data; });
and handle errors in service. note pseudo code.
app.factory 'session', ['$http', ($http) -> getsession: -> return $http.get("/admin/session") .success(data){} .error(){ //... redirect user} ]
Comments
Post a Comment