gpt4 book ai didi

javascript - Angular ui.router 动态侧边栏

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

我现在已经尝试了一整天,但我不知道如何解决这个问题。我的目标是显示一个侧边栏,其中根据其状态填充数据。

假设我在主页上,我可能喜欢这个项目:

  1. 首页项目 1
  2. 首页项目 2

如果我在关于页面,我想要这个项目:

  1. 关于我
  2. 关于我的狗

我希望从服务中获取数据,该服务根据数据的状态将数据返回到 View 。

我尝试使用 ui.router 的解析函数,但我无法在头脑中获得正确的结构来使其工作。

创建了一个 plunkr,展示了我的意思,但没有解决方案,使用 Angular 和 ui.router 创建动态侧边栏时的最佳实践和首选方法是什么?

myapp.config(function($stateProvider) {
$stateProvider
.state('home', {
url: "",
views: {
"content": {
template: "This is the home.content"
},
"sidebar": {
templateUrl: 'sidebar.html',
controller: 'sidebarCtrl'
}
}
})
.state('about', {
url: "/about",
views: {
"content": {
template: "This is the about.content"
},
"sidebar": {
templateUrl: 'sidebar.html',
controller: 'sidebarCtrl'
}
}
})
})

Plunkr here

编辑

无法弄清楚我做错了什么,此代码没有显示 View :

app.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
// For any unmatched url, redirect to root
$urlRouterProvider.otherwise("/");

$stateProvider
.state('root', {
url: '',
'abstract': true,
views: {
'sidebar@': {
templateUrl: '/App/Main/views/shared/sidebars/sidebar.cshtml',
controller: 'app.controllers.views.shared.sidebars.sidebar',
resolve: {
sidebarData: function (sidebarService) {
return sidebarService.getActions();
}
}
}
},
})
.state('root.home', {
url: "/",
views: {
'': {
templateUrl: '/App/Main/views/home/home.cshtml',
controller: 'app.controllers.views.home'
},
'content@root.home': {
templateUrl: '/App/Main/views/home/home-content.cshtml',
controller: 'app.controllers.views.home'
}
}
})
.state('root.about', {
url: "/customers",
views: {
'': {
templateUrl: '/App/Main/views/customers/customers.cshtml',
controller: 'app.controllers.views.customers'
},
'content@root.about': {
templateUrl: '/App/Main/views/customers/customers-content.cshtml',
controller: 'app.controllers.views.customers'
}
}
});
}]);

这是我的家庭和客户 View 的样子:

<div data-ui-view="sidebar">
Loading sidebar view.
</div>
<div data-ui-view="content">
Loading content view.
</div>

最佳答案

您可以尝试为侧边栏实现命名 View 和抽象 View ,以便在其他路由中重用它,您也可以使用解析参数返回您需要的任何动态数据(来自服务、资源等),它可能是这样的:

var myapp = angular.module('myapp', ["ui.router"])
myapp.config(function($stateProvider) {
$stateProvider
.state('root',{
url: '',
abstract: true,
views: {
'sidebar@': {
templateUrl: 'sidebar.html',
controller: 'sidebarCtrl',
resolve:{
sidebarData:function(){
return [{
state: '/stateHere',
text: 'Need'
}, {
state: '/stateHere',
text: 'to'
}, {
state: '/stateHere',
text: 'fill'
}, {
state: '/stateHere',
text: 'this'
}, {
state: '/stateHere',
text: 'with'
}, {
state: '/stateHere',
text: 'dynamic'
}, {
state: '/stateHere',
text: 'data'
}];
}
}
}
})
.state('root.home', {
url: "/",
views: {
'content@': {
template: "This is the home.content"
}
}
})
.state('root.about', {
url: "/about",
views: {
'content@': {
template: "This is the about.content"
}
}
})
});

myapp.controller('sidebarCtrl', ['$scope','sidebarData'
function($scope,sidebarData) {
$scope.sidebarData= sidebarData,

}
]);

编辑:

检查这个工作示例: http://plnkr.co/edit/Nqwlkq1vGh5VTBid4sMv?p=preview

关于javascript - Angular ui.router 动态侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25693908/

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