我的 View 自动重定向到默认(否则) View ,我不知道为什么。当我点击时,它从 View1 转到 View2,然后自动重定向到默认的 View3。我检查了 View2
中的console.log()
View1 -> View2 -> View3
myApp.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('view1', {
url: "/view1",
templateUrl: "App/Views/start/view1.html",
controller: 'MyController'
})
.state('view2', {
url: "/view2/:details",
templateUrl: "App/Views/start/view2.html",
controller: 'MyController'
});
$urlRouterProvider.otherwise('/view3');
});
我正在这样调用 $state.go
。
$state.go('view2', { details: JSON.stringify(response.details) });
我试图通过阻止 $stateChangeStart
上的默认值来拦截它,但遇到了一个错误:
$digest() iterations reached. aborting
这是由于 $urlRouterProvider
和 $stateProvider
之间的交互问题造成的,如 here 所述.但是即使我成功拦截了它,我也不确定这是否是正确的方法。那么到底是什么原因造成的呢?或者我怎样才能找出导致重定向的原因。顺便说一下,我已经检查过 $stateChangeError
,但没有。
myApp.controller('MyController', ['$scope', '$stateParams', '$state', function ($scope, $stateParams, $state) {
$scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (toState.name == "view3" && fromState.name == "view2") {
event.preventDefault();
}
});
});
最佳答案
我认为您的 response.details 具有非 url 编码字符,使路由提供者认为您正在导航到更深的页面,即 var response = {details:'bar/foo'};
会实际上是导航到 /view2/bar/foo
触发你的 otherwise.
关于javascript - Angular View 自动重定向到默认(否则) View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27243789/