gpt4 book ai didi

angularjs - Angular ui-router subview 问题

转载 作者:行者123 更新时间:2023-12-02 22:42:57 25 4
gpt4 key购买 nike

我已经使用 Angular 一两年了,但这是我使用 ui-router 的第一个项目。我遇到了一些 View 和 subview 的问题。该应用程序是一个标准的左侧菜单栏,右侧的 View 根据在菜单栏中单击的内容而变化。

在index.html

<body>
<div ui-view></div>
</body>

在 config.js 文件中,定义路由

 .state("dashboard", {
url: "/dashboard",
templateUrl: "components/dashboard/dashboard.html",
data: {
pageTitle: "Dashboard",
requiresLogin: false
}
})

.state("dashboard.welcome", {
url: "/welcome",
templateUrl: "components/welcome/welcome.html",
data: {
pageTitle: "Welcome",
requiresLogin: false
}
})

在dashboard.html文件中

 <div class="dashboard">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-8">
<div ui-view>

/dashboard 路径加载正确,并将加载左侧导航栏,右侧为空白。但将状态更改为 dashboard.welcome (/welcome) 将不会加载welcome.html 模板。

最佳答案

每当使用ui-router时,您都需要了解状态的概念与路由不同。当您定义子状态时,其定义是相对于其父状态的。在您的场景中,dashboard.welcome 被定义为 dashboard 的子状态。到子状态的路由是相对于父级的,并且是{parent url}/{child url}。因此,您应该使用以下 2 种方法之一来路由到该状态:

使用 $state.go 通过指定状态名称更改状态

$state.go('dashboard.welcome');

使用 $location.path 通过指定 url 更改路线

$location.path('/dashboard/welcome');

关于angularjs - Angular ui-router subview 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074889/

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