javascript 类型错误:无法读取未定义的属性“post”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27340240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Cannot read property 'post' of undefined
提问by Edgar Martinez
I'm getting the following error:
我收到以下错误:
TypeError: Cannot read property 'post' of undefined
at postName (http://127.0.0.1:9000/scripts/controllers/main.js:28:12)
at Scope.$scope.submit (http://127.0.0.1:9000/scripts/controllers/main.js:10:7)
at http://127.0.0.1:9000/bower_components/angular/angular.js:10348:21
at http://127.0.0.1:9000/bower_components/angular/angular.js:18333:17
at Scope.$eval (http://127.0.0.1:9000/bower_components/angular/angular.js:12175:28)
at Scope.$apply (http://127.0.0.1:9000/bower_components/angular/angular.js:12273:23)
at Scope.$delegate.__proto__.$apply (<anonymous>:855:30)
at HTMLFormElement.<anonymous> (http://127.0.0.1:9000/bower_components/angular/angular.js:18332:21)
at HTMLFormElement.jQuery.event.dispatch (http://127.0.0.1:9000/bower_components/jquery/dist/jquery.js:4641:9)
at HTMLFormElement.elemData.handle (http://127.0.0.1:9000/bower_components/jquery/dist/jquery.js:4309:46) angular.js:9563(anonymous function) angular.js:9563(anonymous function) angular.js:7004Scope.$apply angular.js:12275$delegate.__proto__.$apply VM1976:855(anonymous function) angular.js:18332jQuery.event.dispatch jquery.js:4641elemData.handle
My main.js file:
我的 main.js 文件:
'use strict';
'使用严格';
angular.module('sayHiApp')
.controller('MainCtrl', function ($scope) {
// Accepts form input
$scope.submit = function() {
// POSTS data to webservice
postName($scope.input);
// GET data from webservice
var name = getName();
// DEBUG: Construct greeting
$scope.greeting = 'Sup ' + name + ' !';
};
function postName ($scope, $http, dataToPost) {
$http.post('/name', dataToPost).
success(function(data) {
$scope.error = false;
$scope.data = data;
}).
error(function(data) {
$scope.error = true;
$scope.data = data;
});
}
// GET name from webservice
function getName ($scope, $http) {
$http.get('/name').
success(function(data) {
$scope.error = false;
$scope.data = data;
return data;
}).
error(function(data) {
$scope.error = true;
$scope.data = data;
return 'error name';
});
}
});
I'm not sure what this error is referring to? If it's referring to the 'post' method on '$http' then I'm very confused.. Thanks in advance for any help :)
我不确定这个错误指的是什么?如果它指的是 '$http' 上的 'post' 方法,那么我很困惑.. 在此先感谢您的帮助:)
回答by meir shapiro
This is referring to the 'post' method on '$http' as you suggested.
这是指您建议的“$http”上的“post”方法。
You need to add $http as a parameter in the controller function so angular will inject it (just as you did with $scope).
您需要在控制器函数中添加 $http 作为参数,以便 angular 将其注入(就像您使用 $scope 所做的那样)。
I did another change to your code, removed the $scope and $http parameters from the inner functions as they are known in the function because of closure.
我对您的代码进行了另一次更改,从内部函数中删除了 $scope 和 $http 参数,因为它们在函数中是已知的,因为关闭。
angular.module('sayHiApp')
.controller('MainCtrl', function ($scope, $http) {
// Accepts form input
$scope.submit = function() {
// POSTS data to webservice
postName($scope.input);
// GET data from webservice
var name = getName();
// DEBUG: Construct greeting
$scope.greeting = 'Sup ' + name + ' !';
};
function postName (dataToPost) {
$http.post('/name', dataToPost).
success(function(data) {
$scope.error = false;
$scope.data = data;
}).
error(function(data) {
$scope.error = true;
$scope.data = data;
});
}
// GET name from webservice
function getName () {
$http.get('/name').
success(function(data) {
$scope.error = false;
$scope.data = data;
return data;
}).
error(function(data) {
$scope.error = true;
$scope.data = data;
return 'error name';
});
}
});
回答by Edgar Martinez
the function postName
doesn't need to have these vars passed to it
$scope, $http
it only needs the dataToPost
the other two vars are already accessible. Your function should just look like this
该函数postName
不需要将这些变量传递给它
$scope, $http
,它只需要dataToPost
其他两个变量已经可以访问。你的函数应该是这样的
function postName (dataToPost) {
$http.post('/name', dataToPost).
success(function(data) {
$scope.error = false;
$scope.data = data;
}).
error(function(data) {
$scope.error = true;
$scope.data = data;
});
}