簡體   English   中英

AngularJS:無法定義控制器

[英]AngularJS: Unable to define Controller

幾天前,我才剛剛開始使用AngularJS進行開發,這個問題確實讓我很煩。

我不斷收到此錯誤:

Error: ng:areq
Bad Argument
Argument 'NewStudentCtrl' is not a function, got undefined

因為我在其他文件中使用它們,所以所有其他控制器都在工作,只是NewStudentCtrl無法工作。 我嘗試了很多不同的方法,只有一種有效:在使用控制器之前,在HTML文件本身內部使用function NewStudentCtrl ($scope) {}定義控制器。 問題是我想將HTML和JS分成單獨的文件。

請注意,提供的源代碼已簡化很多,可能幾乎沒有縮進或語法錯誤。

<html ng-app="myApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
  <script>var app = angular.module('myApp',[]);</script>
</head>
<body>
  <div ng-controller="NewStudentCtrl">
    <div ng-controller="AccountMenuCtrl">
    </div>

    <script src="js/account-menu.js">
      function AccountMenuCtrl ($scope) {
    }
    </script>

    <div ng-controller="OrtsteileCtrl">
        <option value="{{ortsteil.ID}}" ng-repeat="ortsteil in ortsteile">
          {{ortsteil.Name}}
        </option>
    </div>

    </div> <!-- end NewStudentCtrl -->

  <!-- Loading dependencies -->
  <script src="js/jquery-1.8.3.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/new-student.js">
    var app = angular.module("myApp");

    function NewStudentCtrl ($scope) {
    }
  </script>
  <script src="js/plz.js">
    angular.module('myApp').controller('OrtsteileCtrl', ['$scope', '$http',
      function($scope, $http) {

    }]);
  </script>
</body>
</html>

編輯:從頭開始,我沒有意識到的是,角度對混合它們很滿意。

問題是因為您多次重新創建模塊:

第4行: <script>var app = angular.module('myApp',[]);</script>

第28行: var app = angular.module("myApp");

第34行: angular.module('myApp').controller(...

小提琴: http//jsfiddle.net/uXpqL/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM