AngularJS - 跨引导选项卡使用多个控制器

时间:2017-08-10 19:22:28

标签: angularjs twitter-bootstrap

我想用bootstrap标签分割内容。当我在父元素中附加ng-controller时,除了html中断之外,这种方法很好。有没有办法使用html元素插入ng-controller而不影响在bootstrap中显示标签所需的层次结构?

工作标签布局:

<div ng-app="myApp">
  <div class="tab-content">
    <div id="tab1" class="tab-pane fade in active">{{ tab1_content }}</div> 
    <div id="tab2" class="tab-pane fade in">{{ tab2_content }}</div> 
    <div id="tab3" class="tab-pane fade in">{{ tab3_content }}</div> 
  </div> <!-- all tab-content -->
</div> <!-- end myApp -->

我如何使用我的控制器(通过在所有标签中放置内容来打破标签显示:

<div ng-app="myApp">
  <div class="tab-content">
    <ng-controller ng-controller="test_controller1">   
      <div id="tab1" class="tab-pane fade in active">{{ tab1_content }}</div> 
      <div id="tab2" class="tab-pane fade in">{{ tab2_content }}</div> 
    </ng-controller> <!-- end test_controller1 -->
    <ng-controller ng-controller="test_controller2">   
      <div id="tab3" class="tab-pane fade in">{{ tab3_content }}</div> 
    </ng-controller> <!-- end test_controller2 -->
  </div> <!-- all tab-content -->
</div> <!-- end myApp -->

演示:https://jsfiddle.net/L2b4yLfa/

1 个答案:

答案 0 :(得分:1)

这可以接受吗?

我已为两个标签初始化了相同的控制器。由于它是一个小窗口,代码片段输出将显示移动视图。点击run code snippet -> full page

angular.module('myApp', []);
angular.module('myApp').controller('test_controller1', function($scope) {
	$scope.tab1_content = "Tab One Content";
	$scope.tab2_content = "Tab Two Content";
});

angular.module('myApp').controller('test_controller2', function($scope) {
	$scope.tab3_content = "Tab Three Content";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<nav class="navbar navbar-default">
	<div class="container-fluid">
		<div class="navbar-header">
			<button type="button" class="navbar-toggle pull-left" data-toggle="collapse" data-target="#myNavbar"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
			<a class="navbar-brand" href="#">Title</a>
		</div>
		<div class="collapse navbar-collapse" id="myNavbar">
			<ul class="nav navbar-nav">
				<li class="active"><a data-toggle="tab" href="#tab1">Tab 1</a></li>
				<li><a data-toggle="tab" href="#tab2">Tab 2</a></li> 
				<li><a data-toggle="tab" href="#tab3">Tab 3</a></li> 
			</ul>
			<ul class="nav navbar-nav navbar-right">
				<li>Testing</li>
			</ul>
		</div>
	</div>
</nav>

<div ng-app="myApp">
  <div class="tab-content">
      <div id="tab1" ng-controller="test_controller1" class="tab-pane fade in active">{{ tab1_content }}</div> 
      <div id="tab2" ng-controller="test_controller1" class="tab-pane fade in">{{ tab2_content }}</div> 
      <div id="tab3" ng-controller="test_controller2" class="tab-pane fade in">{{ tab3_content }}</div> 
  </div> <!-- all tab-content -->
</div> <!-- end myApp -->

因此,将来您需要使用服务在两个选项卡的同一控制器之间共享数据。