$ location.path不从控制器内重定向

时间:2015-06-05 05:32:39

标签: angularjs

我正在努力解决在登录个人资料页面后需要重定向(在浏览器中设置新路径)的情况。 以下是我的代码:

/ 在控制器中编写的代码 /

/*Change route to profile page*/
                    var hostname = $location.$$protocol+'://'+$location.$$host;
                    $location.path(hostname+'/patients/profile'); // path take full url including the http

非常感谢任何帮助。

更新


主要模块:

var app = angular.module('patientportal',
    [   
        'ngRoute',
        'authentication',
        'forgotpwd',
    'registerform',
    'ngCookies',
    'profile'
    ]
    ).config(['$routeProvider','$locationProvider','$httpProvider',function($routeProvider,$locationProvider,$httpProvider){
        $locationProvider.html5Mode({
          enabled: true,
          /*
          requireBase: false
          https://docs.angularjs.org/error/$location/nobase
          */
        });
        $httpProvider.defaults.headers.post['Content-Type']='application/x-www-form-urlencoded; charset=UTF-8';
        // send all requests payload as query string
        $httpProvider.defaults.transformRequest = function(data){
        if (data === undefined) {
            return data;
        }
        return serialize(data);
        };
    }]);


var authentication = angular.module('authentication',['patientportal']);
var forgotpwd = angular.module('forgotpwd',['authentication']);
var registerform = angular.module('registerform',['forgotpwd']);
var profile = angular.module('profile',['authentication']);

/*
Module profile 
*/
profile.config(['$routeProvider',function($routeProvider){
  $routeProvider.when('/profile',{
    templateUrl: 'templates/profile.html',
    controller: 'ctrlprofile'
  })

}]);

登录控制器:

authentication.controller('ctrllogin',function ($scope,$http,$rootScope,$cookies,$location,$window){
    $scope.formdata = {};
    $scope.formdata.action = 'patient_portal_login';
    $scope.formdata.is_web = '1'; //set 1 when login through web.
    $scope.formdata.facility_key = facility_key; //from global scope
    $scope.$parent.heading = 'Login'; //setting the heading

    /*make sure there is no old token stored*/
    $cookies.remove('XSRF-TOKEN');

    $scope.submitlogin = function(){
        //If login form is valid
        if($scope.loginform.$valid){

            postdata = $scope.formdata;
            $("#login_patient").mask('Login..');
            request = $http.post(PORTAL_CONTROLLER,postdata)
            .success(function(data, status, headers, config){
                $("#login_patient").unmask();
                if(data.status==1){
                    /*Set rootscope values*/
                    $rootScope.root={
                        html_title:HTML_TITLE_PATIENT_PROFILE,
                        loggedin:true,
                        activeProfile:'active'
                    };

                    /*Set the CRSF cookie*/
                    $cookies.put('XSRF-TOKEN',data.token);

                    /*Store the non sensitive information in sessionstorage*/
                    sessionStorage.setItem('states',JSON.stringify(data.states));
                    noty({timeout:1500,layout:'topCenter',dismissQueue:true,text:data.reason,type:'success',killer:true});

                    /*Change route to profile page*/
                    var hostname = $location.$$protocol+'://'+$location.$$host;
                    //$location.path(hostname+'/patients/profile'); // path take full url including the http
                    $window.location.assign('/patients/profile');
                }
                else{
                    noty({timeout:1500,layout:'topCenter',dismissQueue:true,text:data.reason,type:'error',killer:true});
                }
            })
            .error(function(data, status, headers, config) {
                $("#login_patient").unmask();
                noty({timeout:1500,layout:'topCenter',dismissQueue:true,text:MSG_REQUEST_FAILED,type:'error',killer:true});
            });
        }
    }
});

0 个答案:

没有答案