简体   繁体   中英

$location.path() not working with Angular routes

I realize there are several posts on here regarding this issue. I have tried them all at this point and is probably why code looks pretty bad at this point. What I am trying to do appears quite simple. On submit of a form, I want to redirect to another view via Angular's routing.

THIS IS CALLED FROM THE FORM SUBMIT FUNCTION

$http.post('/api/brackets', jsonData, { headers: headers })

   .success(function(data, status, headers, config) {
   $scope.brackets = data;
   $scope.$on('$routeChangeStart', function(next, current) { 
       $console.log('$routeChangeStart', arguments);
      });
   $location.path('/leaderboard').replace(); ///this is where I am trying to redirecto    to
 $scope.apply().replace();
 $location.path('');                        
})

**THIS IS IN THE ROUTE PROVIDER**

angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider',       function($routeProvider, $locationProvider) {

 // Enable pushState in routes.
$locationProvider.html5Mode(true).hashPrefix('!');
$routeProvider

    // home page
    .when('/', {
        templateUrl: 'views/signin.html',
        controller: 'authController'
    })
    .when('/signin', {
        templateUrl: 'views/signin.html',
        controller: 'authController'
    })
    .when('/signup', {
        templateUrl: 'views/signup.html',
        controller: 'authController'
    })

    .when('/dash', {
        templateUrl: 'views/dashboard.html',
        controller: 'dashController'
    })
    .when('/test', {
        templateUrl: 'views/test.html',
        controller: IndexCtrl
    })
    .when('/profile', {
        templateUrl: 'views/profile.ejs',
        controller: IndexCtrl
    })
    .when('/leaderboard', {
        templateUrl: 'views/leaderboard.html',
        controller: 'leaderboardController' // we might want to make this a partial
    })
    .otherwise({
        redirectTo: '/'
    });

//$locationProvider.html5Mode(true);

}]);

Any help would be greatly appreciated. Thanks.

$http.post('/api/brackets', jsonData, { headers: headers })
.success(function(data, status, headers, config) {
    $scope.brackets = data;
    $scope.$on('$routeChangeStart', function(next, current) { 
        $console.log('$routeChangeStart', arguments);
    });
    $location.path('/leaderboard').replace(); ///this is where I am trying to redirecto    to
    $scope.apply().replace();
    $location.path('');                        
})

This looks really off to me, first you're telling the $location.path to go to /leaderboard, but then two lines later you're telling it to go to nowhere. Do you instead mean to do this:

$http.post('/api/brackets', jsonData, { headers: headers })
.success(function(data, status, headers, config) {
    $scope.brackets = data;
    $scope.$on('$routeChangeStart', function(next, current) { 
        $console.log('$routeChangeStart', arguments);
    });
    $location.path('/leaderboard');              
})

Why are you doing an $apply? Also, why are you doing a .replace?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM