从单个控制器调用多个控制器

时间:2014-04-29 09:19:22

标签: angularjs controller

我的页眉和页脚部分是常见的,视图部分是不同的。

我为index.html页面编写的代码是:

<html lang="en" data-ng-app="dashboard">

<head>  
 <!-- All the Libraries & css--> 
</head>

<body>
<header>  </header>

<data-ng-view>  </data-ng-view>

<footer>   </footer>

</body>
</html>

对于data-ng-view,在主页中我有3 divs每个div都拥有自己的控制器。

主控制器如:

var app = angular.module('dashboard', ['ngRoute'])
    .config(['$routeProvider', function($routeProvider) {
      $routeProvider.
      when('/records', 
              {
                templateUrl: 'view/homepage.html', 
                controller: 'RecordsController'
                }
      ). otherwise({redirectTo: '/records'});  
  }]);

以下是homepage.html

的结构
<div id='main'>
    <article>           
        <div ng-controller="FlipDemoCtrl"> 
            <!-- contents to display -->
            </div>
    </article>
    <nav>
        <div ng-controller="HomePageLinks">  
            <!--  contents to display -->
            </div>
        <hr>
        <ul ng-controller="ResolveProblem">
        <!-- list of contents through ng-repeat -->             
        </ul>
        <hr>
    </nav>
</div>

我的问题是:

我应该如何定义RecordsController,以便通过此控制器我可以调用FlipDemoCtrlResolveProblem&amp; HomePageLinks控制器。

function RecordsController($scope, $http) {

 // How should I write this function to call the multiple controllers

}

1 个答案:

答案 0 :(得分:2)

使用服务在控制器之间共享通用逻辑。然后将服务注入四个控制器,即可访问公共数据。

相关问题