0
我想重定向到另一个状态上stateChangeStart
但event.preventDefault
和$state.go()
不能渲染视图。如果我评论他们两人,它会开始工作。这里是plunker:UI路由器event.preventDefault不会呈现UI视图
var example = angular.module("example", ['ui.router']);
example.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state("root", {
url: "/",
abstract: true,
template: '<ui-view>'
})
.state("root.second", {
abstract: true,
template: '<ui-view>',
})
.state("root.second.a", {
url: "a",
template: "second a template",
})
.state("root.first", {
abstract: true,
template: '<ui-view>'
})
.state("root.first.b", {
url: "b",
template: "first a template"
});
$urlRouterProvider.otherwise("/b");
});
example.run(function ($rootScope, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
event.preventDefault();
$state.go("root.second.a", {}, {notify: false});
});
});
如果我想重定向所有的请求?那又怎么样?另请参阅https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-create-rules-to-prevent-access-to-a-state – norbertpy
如果没有通知:假',我会以无限循环结束。 – norbertpy
我提供的代码和plunker链接显示它适用于所有请求,并且不会以无限循环结束。如果目标状态是'root.second.a',则不会调用条件代码。也许你应该修改你的问题,提供关于你的用例的更多细节,因为这看起来并不理想。 –