[英]$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. 提交表单后,我想通过Angular的路由重定向到另一个视图。
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. 这对我来说真的很奇怪,首先您要告诉$ location.path转到/ leaderboard,但是两行之后您要告诉它无处可去。 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? 您为什么要进行$ apply? Also, why are you doing a .replace? 另外,为什么要执行.replace?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.