AngularJs-参数'AppController'不是一个函数,未定义

时间:2018-10-08 21:29:12

标签: javascript angularjs

我有一个让我感到沮丧的问题,我得到了错误错误:[ng:areq]参数'AppController'不是一个函数,未定义,我无法解决,我尝试了很多方法,但是在所有这些错误中,我都遇到相同的错误,有人知道我该如何解决?

我的html:

<div class="container app-topmargin" ng-app="sampleAngularApp" ng-controller="AppController">
        <div class="row">
          <div class="col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-3 col-md-3">
            <button class="btn btn-primary" ng-click="openDlg()">Open Modal</button>
          </div>
        </div>
        <ng-include src=""></ng-include>

  </div>

我的app.js:

var app = angular.module("sampleAngularApp", []);
app.controller("AppController", ["$scope", function ($scope) {
    $scope.openDlg = function () {
        console.log("clicked here...");

        var dlgElem = angular.element("#modalDlg");
        if (dlgElem) {
            dlgElem.modal("show");
        }
    };
}]);

error

2 个答案:

答案 0 :(得分:0)

尝试将控制器作为您的角度模块数组中的参数传递。

var app = angular.module("sampleAngularApp", ['AppController']);
  app.controller("AppController", ["$scope", function ($scope) {
   $scope.openDlg = function () {
    console.log("clicked here...");

    var dlgElem = angular.element("#modalDlg");
    if (dlgElem) {
        dlgElem.modal("show");
    }
 };
}]);

答案 1 :(得分:0)

您的代码对我来说很好:

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="app.js"></script>
  <div class="container app-topmargin" ng-app="sampleAngularApp" ng-controller="AppController">
        <div class="row">
          <div class="col-xs-12 col-sm-offset-2 col-sm-8 col-md-offset-3 col-md-3">
            <button class="btn btn-primary" ng-click="openDlg()">Open Modal</button>
          </div>
        </div>
        <ng-include src=""></ng-include>
  </div>  
</body>
</html>

app.js

var app = angular.module("sampleAngularApp", []);
app.controller("AppController", ["$scope", function ($scope) {
    $scope.openDlg = function () {
        console.log("clicked here...");
    };
}]);

也许检查一下脚本的加载顺序。