ngRoute不使用外部控制器文件

Get*_*awn 1 controller angularjs ngroute angularjs-ng-route

当我在app.js文件中包含我的控制器时,路由和控制器功能正常工作.它看起来像这样:

的index.html

<!DOCTYPE html>
<html data-ng-app='ckbApp' >    
<head >
</head>

<body>
    <div class="container" >
        <div ng-include="'header.html'"></div>

        <div ng-view class="viewBody">
        </div>

        <div ng-include="'footer.html'"></div>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-route.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="ckbApp.js"></script>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

app.js

'use strict';

var app = angular.module('app', ['ngRoute']);

app.config(['$routeProvider',
    function($routeProvider) {
          $routeProvider. 
          when('/About', {
              templateUrl: 'Views/About.html',
              controller: 'controller'
          }). 
          when('/Blog', {
              templateUrl: 'Views/Blog.html',
              controller: 'controller'
          }).
          when('/FAQ', {
              templateUrl: 'Views/FAQ.html',
              controller: 'controller'
          }). 
          when('/Home', {
              templateUrl: 'Views/Home.html',
              controller: 'controller'
          }). 
          otherwise({
             redirectTo: '/Home' 
          });
}]);

app.controller('controller', ['$scope', '$http', function ($scope, $http) {
    $http.get('data.json').success(function(data) {
        $scope.data = data;
  });    
}]);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将控制器放入外部文件时,没有任何路由工作.我知道控制器仍然有效,因为scope.data仍然为'header.html'加载.我的外部控制器文件看起来像这样:

'use strict';
angular.module('app', []).controller('controller', ['$scope', '$http', function ($scope, $http) {
    $http.get('data.json').success(function(data) {
    });    
}]);
Run Code Online (Sandbox Code Playgroud)

我将控制器源包含在index.html的底部

<script src="/Controllers/controller.js"></script>
Run Code Online (Sandbox Code Playgroud)

我在某处读到ngRoute需要服务器运行,但即使我的服务器运行,它也无法在外部工作,但可以与app.js中包含的控制器一起使用

JB *_*zet 5

angular.module('app', [])
Run Code Online (Sandbox Code Playgroud)

重新定义了名为'app'的模块.您只想获得对其他JS文件中定义的现有模块的引用,并为其添加一个控制器:

angular.module('app')
Run Code Online (Sandbox Code Playgroud)