2013-02-27 60 views
12

我正在尝试AngularJS第一次。当使用$http.post("url", data)函数时,我得到一个Error: $http is not defined控制台消息。

在我的HTML页面的顶部,我在包括所有其他JS导入后的AngularJS。

我也包括因小部件其他JavaScript库的依赖等我的剧本导入部分看起来是这样的:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
<script src="http://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script> 
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script> 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> 

我有我用来发送一些数据到服务器,控制器:

function FormCtrl($scope) { 
    $scope.sendData = function(){ 
     var data = "firstName=" + $scope.firstName + "&" + 
      "lastName=" + $scope.lastName + "&" + 
      "address=" + $scope.address + "&" + 
      "postcode=" + $scope.postcode; 

     $http.post("/data/person/put", data); 
    }; 
} 

sendData函数附加到按钮上。所有工作正常,直到调用$http.post(...)在此时控制台输出错误。

完整的错误列表:

Error: $http is not defined 
[email protected]://localhost:8080/angularjs/:96 
Mc/x/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:71 
ec[c]</</</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:142 
e.prototype.$eval[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:87 
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:87 
ec[c]</</<@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:142 
[email protected]://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js:63 
c.event.add/[email protected]://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js:57 

https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js 
Line 61 

我必须做别的事情AngularJS配置为使用$http.post功能?

我解除了使用直接从AngularJS 快捷方法这里的文档部分:http://docs.angularjs.org/api/ng.$http

谁能帮助?

感谢 亚当

回答

23

function FormCtrl($scope) {应该function FormCtrl($scope, $http) {

您需要注入控制器所需的所有服务,在这种情况下,您使用的是$scope$http服务,但您只注入了造成错误的原因$scope

例:

function FormCtrl($scope, $http) { 
    .... 
} 
FormCtrl.$inject = ['$scope', '$http']; 

你可以阅读关于注射微小here并发症一张纸条,读A Note on Minification

+0

尝试过了,现在完全工作。感谢您的快速回复。这么快,我甚至不能接受你的答案。但我会。再次感谢! – 2013-02-27 12:46:28

+1

你可以在http://docs.angularjs.org/tutorial/step_05上阅读关于注射并发症的注意事项 – 2013-02-27 12:47:29