正确地声明角度控制器,而不是两次

时间:2016-01-23 06:42:58

标签: angularjs

在学习/阅读angular docs时,它会警告使用ng-controller声明控制器再次

这是否描述了我的代码,声明 MainMenuCtrl 两次。首先在app.js中,第二次在index.html的body标签中?感谢



//---index.html-------------------------------
<!doctype html>
<html lang="en" ng-app="angApp">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/index.css">
    <base href="http://localhost:63342/project_student/">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.min.js"></script>
    <script src="https://code.angularjs.org/1.5.0-rc.1/angular-animate.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers/controllers.js"></script>
    <script src="js/controllers/headerCtrl.js"></script>
    <script src="js/controllers/first_page.js"></script>
    <script src="js/controllers/second_page.js"></script>
    <meta name="viewport" content="width=device-width" />
</head>

<body ng-controller="MainMenuCtrl">   //<<------1st declare-----------------
<header>
    <button class="menuLeft" type="button" ng-click="toggleList()">&#9776;</button>
    <label id="pageTitle" class="pageTitle">Select item from list</label>
    <button class="menuRight" type="button">&#8942;</button>
</header>

<main class="listview" ng-hide="showListView" ng-view></main>

<footer id="footer" class="footer">
    <ul class="horizontal-style">
        <li><button type="button">NO</button></li>
        <!--<li><button type="button">EXTRA</button></li>-->
        <li><button type="button">YES</button></li>
    </ul>
</footer>

</body>
</html>
&#13;
//---app.js-------------------------------
 (function () {                            //y010
     'use strict';
     angular
        .module('angApp', ['ngRoute', 'MainMenuCtrl']) //<<------2nd declare-----------
        .config(['$routeProvider', routeProvider]);     //y024
 })();

 function routeProvider ($routeProvider) {
     $routeProvider.when('/menuItems', {
         url: "/menuItems",
         templateUrl: 'views/mainMenu.html',
         controller: 'MainMenuCtrl' //<<-------- called-------------
     }).when('/first_page', {
         url: "/first_page",
         templateUrl: 'views/first_page.html',
         controller: 'FirstPageController'
     }).when('/second_page', {
         url: "/second_page",
         templateUrl: 'views/second_page.html',
         controller: 'SecondPageController'
     })
     .otherwise({      //home page
         redirectTo: '/menuItems'
     });
 }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

不,你只宣告一次。在模板中,您只是声明主体及其内容应该来自在控制器的范围