我知道这里有几篇关于这个问题的帖子。我已经尝试了所有这些,这可能就是代码此时看起来很糟糕的原因。我想做的事情看起来很简单。提交表单后,我想通过 Angular 的路由重定向到另一个 View 。
这是从表单提交函数调用的
$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?
关于javascript - $location.path() 不适用于 Angular 路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24002648/