参数'cotroller'不是函数,未定义

时间:2016-03-24 09:45:08

标签: javascript html angularjs

这是我的HTML文件

<head>
    <meta charset="UTF-8"/>
    <title>CBIR</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="../js/angular.js"></script>
    <script src="../js/AngularCotroller.js"></script>
    <script src="../js/ApiCallService.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div ng-app="App">
    <div ng-controller="AppController">
         <div class="form-group row">
            <button id="btnGetKey" class="btn btn-default" ng-click="btnGetKey()">Get Key</button>
            <p> {{message}}</p>
        </div>


        <br>
    </div>
  </div>
</div>
</body>
</script>

这是AngularController.js文件

var angularmodule = angular.module('App', []);

angularmodule.controller('AppController', function ($scope, $http, ApiCall)      {
//Intital message value
$scope.message = "Don't Give up";
$scope.btnGetkey = function () {

    var result = ApiCall.GetKeyFromServer().success(function (data) {
        var data = $.parseJSON(JSON.parse(data));
        $scope.message = data;
        $scope.message = "123123123";
    });
};


});

这是ApiServiceCall文件

angularmodule.service('ApiCall', ['$http', function ($http) 
{
var result;

// This is used for calling get methods from web api
this.GetKeyFromServer = function () {
    result = $http.get('http://localhost:8090/CBIR/checkReturn').success(function (data, status) {
        result = (data);
    }).error(function () {
        alert("Something went wrong");
    });
    return result;
};

}]);

当我加载索引文件时错误:[ng:areq]参数'AppController'不是函数,未定义 http://errors.angularjs.org/1.2.16/ng/areq?p0=AppController&p1=not%20a%20function%2C%20got%20undefined 我收到这个错误。请帮忙

1 个答案:

答案 0 :(得分:4)

您使用一个参数,但实际函数使用三个参数:

['$scope', function ($scope, $http, ApiCall)

您需要指定所有这些,如下所示:

['$scope', '$http', 'ApiCall', function ($scope, $http, ApiCall)

或者如果您不打算最小化代码,请使用:

function ($scope, $http, ApiCall)

正如@Slytherin建议的那样,您遇到的另一个错误是您的服务文件:

  • 你有一个拼写错误:AnguarModule!= angular.module
  • 即使它不是拼写错误,您也可以重新复制您的模块,而不是引用它(差异是第二个参数 - see the docs