javascript - angularjs app to redirect after login -


currently, when users logs in, login page does't redirect homepage.

'use strict';  angular.module('myapp').service('auth', function auth($http, api_url, authtoken, $state, $window, $q) {   function authsuccessful(res) {     authtoken.settoken(res.token);     $state.go('main');   }    this.login = function (email, password) {     return $http.post(api_url + 'login', {       email: email,       password: password     }).success(authsuccessful);   }    this.register = function (email, password) {     return $http.post(api_url + 'register', {       email: email,       password: password     }).success(authsuccessful);   } 

however, have set $state.go redirect main. problem? why not redirecting?

annex

here login.js controller, how looks:

angular.module('myapp').controller('loginctrl', function ($scope, alert, auth, $auth) {    $scope.submit = function () {     $auth.login({       email: $scope.email,       password: $scope.password     })       .then(function(res) {         var message = 'thanks coming ' + res.data.user.email + '!';         if (!res.data.user.active)           message = 'just reminder, please activate account :)';          alert('success', 'welcome', message);       })       .catch(handleerror);   }   // forgot include error handler in code:   function handleerror(err) {     alert('warning', 'oops there problem!', err.message);   } }); 

since async action, angular doesn't know when action finishes , when update $scope. work you'll need manually call $scope.apply(), since don't have access $scope in service, need move redirection logic (i.e. $state.go('main')) inside controller, , call this:

angular.module('myapp').controller('loginctrl', function($scope, auth, $state) {   function redirect(res) {     $state.go('main');      // manually update scope     $scope.$apply();   }    auth.login(email, password)       .success(redirect); }); 

edit: integrate given controller

angular.module('myapp').controller('loginctrl', function ($scope, alert, auth, $auth) {      $scope.submit = function () {         $auth.login({           email: $scope.email,           password: $scope.password         })             .then(function(res) {               var message = 'thanks coming ' + res.data.user.email + '!';               if (!res.data.user.active) {                 message = 'just reminder, please activate account :)';               }                alert('success', 'welcome', message);                return null;             })             .then(function() {               $state.go('main');                // manually update scope               $scope.$apply();             })             // google replacement of above commented out code bit             .catch(handleerror);       }     }); 

edit 2: use $timeout instead of $scope.$apply don't $digest error.

angular.module('myapp').controller('loginctrl', function ($scope, alert, auth, $auth, $timeout) {      ...     .then(function() {       // $timeout calls $scope.$apply() default,        // in safely manner - never throws '$digest in progress' exception        $timeout(function() {          $state.go('main');       });     })     ... 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -