ngRoute - 单独文件中的多个控制器

时间:2016-02-02 11:19:04

标签: angularjs angularjs-ng-route

我现在正在将我的网络应用程序更改为单页面网络应用程序。 我按照这个教程进行了教程。link

这是一个示例script.js

// script.js

// create the module and name it scotchApp
    // also include ngRoute for all our routing needs
var scotchApp = angular.module('scotchApp', ['ngRoute']);

// configure our routes
scotchApp.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'pages/about.html',
            controller  : 'aboutController'
        })

        // route for the contact page
        .when('/contact', {
            templateUrl : 'pages/contact.html',
            controller  : 'contactController'
        });
});

// create the controller and inject Angular's $scope
scotchApp.controller('mainController', function($scope) {
    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

scotchApp.controller('aboutController', function($scope) {
    $scope.message = 'Look! I am an about page.';
});

scotchApp.controller('contactController', function($scope) {
    $scope.message = 'Contact us! JK. This is just a demo.';
});

由于我的控制器很大且数量更多,我将它们分成单独的文件。

但现在控制器无法识别..

2 个答案:

答案 0 :(得分:13)

  

更新了分离的控制器和html视图。

     

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script type="text/javascript" src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js'></script>
    <script type="text/javascript" src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-route.js'></script>
    <script type="text/javascript" src='./app.js'></script>
    <script type="text/javascript" src='./HomeController.js'></script>
    <script type="text/javascript" src='./AboutController.js'></script>
    <script type="text/javascript" src='./ContactController.js'></script>
  </head>
  <body ng-app="myAPP">
      <div>
          <div id="navigation">
              <a href="#/home">Home</a>
              <a href="#/about">About</a>
              <a href="#/contact">Contact</a>
          </div>
          <div ng-view></div>
       </div>
  </body>
</html>
  

app.js

'use strict';

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

myAPP.config(['$routeProvider',
    function (
        $routeProvider
    ) {
          $routeProvider.
              when('/home', {
                  templateUrl: 'pages/home.html',
                  controller: 'HomeController'
              }).
              when('/about', {
                  templateUrl: 'pages/about.html',
                  controller: 'AboutController'
              }).
              when('/contact', {
                  templateUrl: 'pages/contact.html',
                  controller: 'ContactController'
              }).
              otherwise({
                  redirectTo: '/home'
              });
}]);
  

HomeController.js

angular.module('myAPP')
    .controller('HomeController', function ($scope) {
       // create a message to display in our view
       $scope.message = 'Everyone come and see how good I look!';
    });
  

AboutController.js

angular.module('myAPP')
 .controller('AboutController', function ($scope) {
        $scope.message = 'Look! I am an about page.';
 });
  

ContactController.js

angular.module('myAPP')
 .controller('ContactController', function ($scope) {
        $scope.message = 'Contact us! JK. This is just a demo.';
 });
  

页/ About.html

<h1> About </h1>
{{message}}
  

页/ Contact.html

<h1> Contact </h1>
{{message}}
  

页/ Home.html中

<h1> Home </h1>
{{message}}
  

文件夹视图:

enter image description here

  

结果:

enter image description here

答案 1 :(得分:0)

在我看来,除非您忘记将控制器包含在索引文件中,否则它应该有效...