Javascript 用于选项卡的 AngularJS 控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31753558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS controller for a tab
提问by Nikhil
I have three tabs in my page. I'm using tabset
and tab
according to Angular Bootstrap Docs.
我的页面中有三个标签。我正在使用tabset
并tab
根据Angular Bootstrap Docs。
I set a controller for the <div>
which has the tabset
as
我为<div>
具有tabset
as的控制器设置了一个控制器
<div ng-controller="Tabs" class="panel panel-default" id="tabs-panel">
<tabset class="panel-body">
<tab heading="Tab 1"> </tab>
<tab heading="Tab 2"> </tab>
<tab heading="Tab 3"> </tab>
</tabset>
</div>
But, when I try to add another controller for my 2nd tab as
但是,当我尝试为我的第二个选项卡添加另一个控制器时
<div ng-controller="Tabs" class="panel panel-default" id="tabs-panel">
<tabset class="panel-body">
<tab heading="Tab 1"> </tab>
<tab heading="Tab 2" ng-controller="Tab2> </tab>
<tab heading="Tab 3"> </tab>
</tabset>
</div>
I now find that the heading is not displayed and I can no longer click the Tab2.
我现在发现标题没有显示,我无法再单击 Tab2。
Why is that? How to get back the same functionality?
这是为什么?如何恢复相同的功能?
Is this the right way to add another controller in an existing controller?
这是在现有控制器中添加另一个控制器的正确方法吗?
My app.js:
我的app.js:
var myApp = angular.module('myApp',['ui.bootstrap']);
myApp.controller('Tabs', function ($scope) {
});
myApp.controller('Tab2', function ($scope) {
});
采纳答案by AWolf
I think there are at least three ways you could organize your controller code:
我认为至少可以通过三种方式组织控制器代码:
- Everything in one
TabController
- Custom directive for each tab
- Add your tab logic to the tab callback function.
- 一切合二为一
TabController
- 每个选项卡的自定义指令
- 将选项卡逻辑添加到选项卡回调函数。
Please have a look at the demo below or here at jsfiddle.
请查看下面的演示或在jsfiddle 中查看演示。
It's the ui-bootstrap
example code with the above mentioned points added.
这是ui-bootstrap
添加了上述要点的示例代码。
angular.module('demoApp', ['ui.bootstrap'])
.controller('TabsDemoCtrl', TabsController)
.directive('sepecialTab', SpecialTab);
function TabsController($scope, $window) {
$scope.tabs = [{
title: 'Dynamic Title 1',
content: 'Dynamic content 1'
}, {
title: 'Dynamic Title 2',
content: 'Dynamic content 2',
disabled: true
}];
$scope.alertMe = function () {
setTimeout(function () {
$window.alert('You\'ve selected the alert tab!');
});
};
$scope.thirdTabCallback = function () {
$scope.test = 'I\'m the third tab callback';
$scope.clickme = function () {
$window.alert('third tab only function');
};
};
}
function SpecialTab() {
return {
restrict: 'A',
controller: function ($scope) {
console.log('Special tab ctrl, runs on start.');
$scope.hello = 'hello from special tab controller';
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.1/ui-bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.13.1/ui-bootstrap-tpls.js"></script>
<div ng-app="demoApp" ng-controller="TabsDemoCtrl">
<p>Select a tab by setting active binding to true:</p>
<p>
<button class="btn btn-default btn-sm" ng-click="tabs[0].active = true">Select second tab</button>
<button class="btn btn-default btn-sm" ng-click="tabs[1].active = true">Select third tab</button>
</p>
<p>
<button class="btn btn-default btn-sm" ng-click="tabs[1].disabled = ! tabs[1].disabled">Enable / Disable third tab</button>
</p>
<hr />
<tabset>
<tab heading="Static title">Static content</tab>
<tab heading="Static title 2" sepecial-tab="">Static content2 {{hello}}</tab>
<tab heading="Static title 3" select="thirdTabCallback()">Static content3 {{test}}
<button ng-click="clickme()">click me</button>
</tab>
<tab ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active" disable="tab.disabled">{{tab.content}}</tab>
<tab select="alertMe()">
<tab-heading> <i class="glyphicon glyphicon-bell"></i> Alert!</tab-heading>I've got an HTML heading, and a select callback. Pretty cool!</tab>
</tabset>
</div>
回答by user3435330
Here a custom directive to use in tabs, so I can use custom controllers for each tab.
这是在选项卡中使用的自定义指令,因此我可以为每个选项卡使用自定义控制器。
angular.module('myModule', ['ui.bootstrap'])
.directive('tabController', function() {
return {
restrict: 'A',
controller: '@',
name: 'tabController',
}
})
.controller('MyCustomController', function() {
var vm = this;
vm.title = "Hey, I am The first controller";
})
.controller('MyCustomController2', function() {
var vm = this;
vm.title = "Hey, I am the second controller!";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<html>
<div ng-app="myModule">
<uib-tabset type="tabs">
<uib-tab tab-controller="MyCustomController as ctrl">
<uib-tab-heading>
<span ng-bind="ctrl.title"></span>
</uib-tab-heading>
<p ng-bind="ctrl.title"></p>
</uib-tab>
<uib-tab tab-controller="MyCustomController2 as ctrl2">
<uib-tab-heading>
<span ng-bind="ctrl2.title"></span>
</uib-tab-heading>
<p ng-bind="ctrl2.title"></p>
</uib-tab>
</uib-tabset>
</div>