我正在使用 MEAN.js 样板,您可以找到完整的代码here https://github.com/meanjs/mean.
从列表中选择一篇文章后,我尝试向呈现的页面添加 2 个新选项卡。
对于此任务,我决定对 Angular.js 使用 UI-Router 和 UI-Bootstrap。
这两个选项卡无法正常工作,我可以在它们之间切换并正确查看其内容,但有时当我返回并选择文章列表菜单项时,我会看到一个带有两个选项卡的空白页面,没有其他内容。
以下是对 view-article.client.view.html 文件的更改,以包含 2 个新选项卡(之前的内容已复制到包含新选项卡部分内容的 2 个文件):
<div ng-controller="ArticlesController">
<tabset>
<tab
ng-repeat="t in tabs"
heading="{{t.heading}}"
select="go(t.route)"
active="t.active">
</tab>
</tabset>
<div ui-view></div>
</div>
我已将以下几行代码插入到文章控制器中:
$scope.tabs = [
{ heading: 'SubA', route:'viewArticle.SubA', active:false },
{ heading: 'SubB', route:'viewArticle.SubB', active:false }
];
$scope.go = function(route){
$state.go(route);
};
$scope.active = function(route){
return $state.is(route);
};
$scope.$on('$stateChangeSuccess', function() {
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
这是route.js
'use strict'
angular.module('articles').config(['$stateProvider',
function($stateProvider) {
$stateProvider.
state('listArticles', {
url: '/articles',
templateUrl: 'modules/articles/views/list-articles.client.view.html'
}).
state('createArticle', {
url: '/articles/create',
templateUrl: 'modules/articles/views/create-article.client.view.html'
}).
state('viewArticle', {
url: '/articles/:articleId',
templateUrl: 'modules/articles/views/view-article.client.view.html'
}).
state('editArticle', {
url: '/articles/:articleId/edit',
templateUrl: 'modules/articles/views/edit-article.client.view.html'
}).
state('viewArticle.SubA', {
url: '/SubA',
templateUrl: 'modules/articles/views/view-article.client.view.SubA.html'
}).
state('viewArticle.SubB', {
url: '/SubB',
templateUrl: 'modules/articles/views/view-article.client.view.SubB.html'
});
}
]);