繁体   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