gpt4 book ai didi

javascript - ui-router event.preventDefault 不会渲染 ui-view

转载 作者:行者123 更新时间:2023-12-03 08:53:32 25 4
gpt4 key购买 nike

我尝试在 stateChangeStart 上重定向到另一个状态,但 event.preventDefault$state.go() 无法渲染 View 。如果我注释掉它们,它就会开始工作。这是plunker :

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});
});

});

最佳答案

设置notify为假prevents the view directive from doing it's job 。而不是使用 notify: false ,我认为您这样做是为了避免 $stateChangeStart函数,您可以有条件地调用 $state.go() .

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
if (toState.name !== 'root.second.a') {
event.preventDefault();
$state.go('root.second.a');
}
});

骗子:http://plnkr.co/edit/tJPxXGir5YSSPG5jGodn?p=preview

关于javascript - ui-router event.preventDefault 不会渲染 ui-view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32597326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com