【发布时间】:2014-07-11 09:30:26
【问题描述】:
如何在 ui-sref 中放置指向子状态的链接并在下方放置一个空的 ui-view 时加载默认子状态。我希望 ui-view 在页面加载时默认加载第一个子状态,并通过用户单击 ui-sref 链接,他/她在状态之间切换。
以下是解释我的场景的完整代码:
<div class="apps_head settings_top_head1">
Favorities
<a href="javascript:;" onclick="setRow1Toggle()"><img src="img/apps_menu.png"/></a>
<div id="row1Dropdown" style="border: solid; display: none">
<ul>
<li><a onclick="setRow1Toggle()" ui-sref="dashboard.mainContent.favorites" href="javascript:;" style="color: black">Favorities</a></li>
<li><a onclick="setRow1Toggle()" ui-sref="dashboard.mainContent.passwords" href="javascript:;" style="color: black">Passwords</a></li>
</ul>
</div>
<div ui-view></div>
//=============================================== ======================================= //这里是 UI-Router 配置:
var myApp = angular.module('myApp', [
//'ngRoute',
'ui.router',
'JungleLockControllers'
]).run(function($rootScope, $state){
$rootScope.$state = $state;
});
myApp.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home',{
url:'/home',
templateUrl:'partials/home.html',
controller : 'HomepageController'
})
.state('career',{
url:'/career',
templateUrl:'partials/career.html'
})
.state('faq',{
url:'/faq',
templateUrl:'partials/faq.html'
})
.state('companyOverview',{
url:'/companyOverview',
templateUrl:'partials/companyOverview.html'
})
.state('press',{
url:'/press',
templateUrl:'partials/press.html'
})
.state('terms',{
url:'/terms',
templateUrl:'partials/terms.html'
})
.state('dashboard',{
url:'/dashboard',
views:{
'':{templateUrl:'partials/dashboard.html'},
'notifications@dashboard':{templateUrl:'partials/dashboard.notifications.html'}
}
})
.state('dashboard.mainContent',{
url:'/mainContent',
//templateUrl:'partials/dashboard.mainContent.html'
views: {
'':{templateUrl:'partials/dashboard.mainContent.html'},
'contentHeader@dashboard.mainContent':{templateUrl:'partials/dashboardContentHeader.html'},
'row1@dashboard.mainContent':{templateUrl:'partials/row1.dashboard.html'},
'row2@dashboard.mainContent':{templateUrl:'partials/row2.dashboard.html'},
'row3@dashboard.mainContent':{templateUrl:'partials/row3.dashboard.html'},
//'favorites@dashboard.mainContent':{templateUrl: "partials/favouriteContent.html"},
//'passwords@dashboard.mainContent':{templateUrl: "partials/passwordsContent.html"},
'sponsored@dashboard.mainContent':{templateUrl: "partials/sponsoredContent.html"},
'browse@dashboard.mainContent':{templateUrl: "partials/browseContent.html"},
}
})
.state('dashboard.mainContent.favorites',{
url:'/favorites',
templateUrl:'partials/favouriteContent.html'
})
.state('dashboard.mainContent.passwords',{
url:'/passwords',
templateUrl:'partials/passwordsContent.html'
})
.state('dashboard.settings',{
url:'/settings',
templateUrl:'partials/dashboard.settings.html'
})
// .state('settings',{
// url:'/settings',
// templateUrl:'partials/dashboard.settings.html'
// })
.state('logout',{
url:'/logout',
templateUrl:'partials/logout.html'
})
/*
Template for adding new empty state
.state('',{
url:'',
templateUrl:''
})
*/
;
$urlRouterProvider.otherwise('/home');
});
【问题讨论】:
-
发布您的 ui-router 配置以及 plunker 上的工作版本会更好
-
特指状态:dashboard.mainContent
-
@jack.the.ripper 这样的场景,如果我参考 scotch.io ui-router 教程。我想在这个 plunker 上完成以下操作:[link])plnkr.co/edit/IzimSVsstarlFviAm7S7?p=preview)。这里 home 状态有两个子状态,1) home.list ,2) home.paragrapgh,所以当页面加载时,默认没有加载任何状态,当用户点击其中一个 ui-sref 时,相应的状态被加载。 我想要的是默认加载 home.list,并且用户仍然可以通过 ui-sref 更改子状态
标签: angularjs angular-ui-router