I'm trying out AngularJS for the first time. When using the $http.post("url", data)
function, I'm getting a Error: $http is not defined
console message.
我第一次试着用英语说英语。当使用http美元。post(“url”,data)函数,我得到一个错误:$http没有定义控制台消息。
At the top of my HTML page I am including AngularJS after all other JS imports.
在HTML页面的顶部,在所有其他JS导入之后,我将包含AngularJS。
I have also included other JavaScript libraries due to widget dependencies etc. My script imports section looks like this:
由于小部件依赖等原因,我还包括了其他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>
I have a controller which I'm using to send some data to the server:
我有一个控制器,用来向服务器发送数据:
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);
};
}
The sendData
function is attached to a button. All works fine until the call to $http.post(...)
at which point the console outputs the error.
sendData函数附加到一个按钮上。在调用$http.post(…)时,所有的工作都是正常的,控制台输出错误。
The full error listing is:
完整的错误列表是:
Error: $http is not defined
$scope.sendData@http://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@https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js:87
e.prototype.$apply@https://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
c.event.handle@http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js:63
c.event.add/o@http://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
Do I have to do something else to configure AngularJS to use the $http.post
function?
我是否需要做一些其他的事情来配置AngularJS来使用$http。发布函数?
I lifted the usage straight from the AngularJS Shortcut Methods section of the documentation here: http://docs.angularjs.org/api/ng.$http
我直接从文档的AngularJS快捷方式小节中提取了用法:http://docs.angularjs.org/api/ng.$http
Can anyone help?
谁能帮忙吗?
Thanks Adam
由于亚当
1 个解决方案
#1
23
function FormCtrl($scope) {
should be function FormCtrl($scope, $http) {
.
函数FormCtrl($scope){应该是函数FormCtrl($scope, $http){。
You need to inject all the services required to the controller, in this case you are using $scope
and $http
service, but you have injected only $scope
that is the cause of the error.
您需要向控制器注入所需的所有服务,在本例中,您使用的是$scope和$http服务,但是您只注入了$scope,这是错误的原因。
Ex:
例:
function FormCtrl($scope, $http) {
....
}
FormCtrl.$inject = ['$scope', '$http'];
You can read a note about complications in injection with minification here, read A Note on Minification
你可以在这里读一下注射时并发症的说明,读一下关于缩小的说明
#1
23
function FormCtrl($scope) {
should be function FormCtrl($scope, $http) {
.
函数FormCtrl($scope){应该是函数FormCtrl($scope, $http){。
You need to inject all the services required to the controller, in this case you are using $scope
and $http
service, but you have injected only $scope
that is the cause of the error.
您需要向控制器注入所需的所有服务,在本例中,您使用的是$scope和$http服务,但是您只注入了$scope,这是错误的原因。
Ex:
例:
function FormCtrl($scope, $http) {
....
}
FormCtrl.$inject = ['$scope', '$http'];
You can read a note about complications in injection with minification here, read A Note on Minification
你可以在这里读一下注射时并发症的说明,读一下关于缩小的说明