簡體   English   中英

$ location.path()不適用於Angular路線

[英]$location.path() not working with Angular routes

我意識到這里有關於此問題的幾篇文章。 在這一點上,我已經嘗試了所有方法,這可能就是為什么代碼在這一點上看起來很糟糕的原因。 我想做的事情看起來很簡單。 提交表單后,我想通過Angular的路由重定向到另一個視圖。

這是從表單提交功能中得知的

$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('');                        
})

**這是路由提供程序中的**

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);

}]);

任何幫助將不勝感激。 謝謝。

$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('');                        
})

這對我來說真的很奇怪,首先您要告訴$ location.path轉到/ leaderboard,但是兩行之后您要告訴它無處可去。 您是否打算這樣做:

$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');              
})

您為什么要進行$ apply? 另外,為什么要執行.replace?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM