我正在使用MEAN.js样板文件,你可以找到整个代码
here.
我尝试在从列表中选择其中一篇文章后向页面添加2个新选项卡.
为此,我决定为Angular.js使用UI-Router和UI-Bootstrap.
2个选项卡无法正常工作,我可以在它们之间切换并正确查看其内容,但偶尔当我返回并选择文章列表菜单项时,我得到一个带有2个选项卡的空白页面,而不是其他任何内容.
以下是对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' }); } ]);
这与angular-ui bootstrap tab指令和select()回调有关.看起来,当离开tab2时,正在调用tab2中的select()回调.
我变了:
`<tab ng-repeat="t in tabs" heading="{{t.heading}}" select="go(t.route)" active="t.active"> `</tab>
至:
`<tab ng-repeat="t in tabs" heading="{{t.heading}}" ui-sref="{{t.route}}" active="t.active"> </tab>`
你的演示现在正常工作.