mercredi 1 juin 2016

How to reload state and add a parse the parameter to router?

I want to reload my state after I login, and parse the ID that I get from local storage into the url by using this codes, Controller

.controller('BodyController', ['Account', '$scope', '$state', '$stateParams', '$rootScope', 'AuthService', function(Account, $scope, $state, $stateParams, $rootScope, AuthService){

$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;

if(Account.isAuthenticated() == false) {
    $scope.loggedIn = false;
}

if($scope.loggedIn === false){
    $scope.layout = 'login';
    $state.go('app.mainUnreg');
}
else{
    $scope.loggedIn = true;
    $scope.layout = 'styles';
    $state.go('app.mainReg');
}
}])
.controller('LoginController', ['Account','$scope', '$state', '$stateParams', 'AuthService', '$rootScope', '$route', '$routeParams', '$location', function (Account, $scope, $state, $stateParams, AuthService, $rootScope, $currentUserId, $route, $routeParams, $location) {

$currentUserId = Account.getCurrentId();
$scope.$stateParams = $stateParams;

$scope.loginData = {
  email: 'testing@gmail.com',
  password: 'Abc123456',
};

$scope.doLogin = function() {
    console.log('Logging in as ', $scope.loginData.email);

    AuthService.login($scope.loginData)
    .then(function() {
        $scope.loggedIn = true;
        $stateParams.userID = Account.getCurrentId();
        console.log($stateParams.userID);
        $state.go('app.mainReg', {param: Account.getCurrentId()});
        $route.reload();
    });
    };
}])

and I have this kind of stateProvider in my app.js

.state('app', {
        url:'/',
        abstract: true,
        vie

ws:{
                'bodyLogin': {
                    templateUrl : 'views/login/body.html',
                    controller  : 'BodyController'
                },
                'bodyMain': {
                    templateUrl : 'views/main/body.html',
                    controller  : 'BodyController'
                }
            }
        })
.state('app.mainUnreg', {
            url:'home',
            views: {
                'content@app': {
                    templateUrl : 'views/login/home.html',
                    controller  : ''
                }
            }
        })
.state('app.mainReg', {
            url:':userID',
            views: {
                'header@app': {
                    templateUrl : 'views/main/header.html',
                    controller  : ''
                },
                'sidebar@app': {
                    templateUrl : 'views/main/sidebar.html',
                    controller  : ''
                },
                'middle@app': {
                    templateUrl : 'views/main/home.html',
                    controller  : ''
                },
                'footer@app': {
                    templateUrl : 'views/main/footer.html',
                    controller  : ''
                }
            },
            controller: 'MainController',
            resolve: {
                myResolve1:
                function($http, $stateParams) {
                    return $http.get("/api/accounts/"+ Account.getCurrentId());
                }
            }
        })

and I separate each state by div

<div ng-if="!loggedIn" ui-view="bodyLogin" class="site-wrapper" ui-sref-active="app.mainUnreg"></div>

<div ng-if="loggedIn" ui-view="bodyMain" ui-sref-active="app.mainReg"></div>

and inside my AuthService,

function login(loginData) {
    var params = { rememberMe: loginData.rememberMe };
    var credentials = {
        email: loginData.email,
        password : loginData.password
    };
    return User
        .login(params, credentials)
        .$promise.then(function(response){

            $rootScope.currentUser = {
                tokenId: response.id,
                email: response.email
            };
        },
        function(response) {
            console.log(response);
        });
}

but, I seems that it didn't give any effect, even I have logged in, I need to manually reload the page to let me enter app.mainReg and the parameter :userID didn't parse into my url

anyway, I use lb-service from Angular SDK to generate my standard service

Does anyone have any idea where I made some mistake, so I after I login, the controller / state could automatically reload and parse the currentUserId into url parameter?

Aucun commentaire:

Enregistrer un commentaire