Angularjs $ http.get不起作用 [英] Angularjs $http.get does not work
问题描述
我的目的是消耗AngularJS一个REST Web服务,我现在正在做一些试验。低于code未工作,抛出以下异常。
你能帮我identifty问题?
My purpose is to consume a REST web service in AngularJS and I am making some trials right now. Below code is not working and throwing following exception. Can you help me identifty the problem?
先谢谢了。
function getUsersFromLocal($scope,$http)
{
$http.get('http://localhost:8080/people').
success(function(data) {
$scope.data = data;
});
return data;
}
错误是:类型错误:无法读取属性未定义'得到'
在getUsersFromLocal。
Error is: TypeError: Cannot read property 'get' of undefined at getUsersFromLocal.
该服务访问,我测试了它通过一些REST的客户。
The service is accessible and I tested it through some REST clients.
推荐答案
如果我理解正确的 getUsersFromLocal
函数内部控制,基本功能参数杀 $范围
, $ HTTP
对象的存在,你需要从参数和放大器将其删除;也去掉这是外面return语句 $ HTTP
这是行不通的反正。
If I understood correctly getUsersFromLocal
function is inside controller, basically the function parameters are killing the $scope
, $http
object existence, You need to remove them from parameter & Also removed the return statement which was outside $http
which won't work in anyways.
code
app.controller('mainCtrl', function() {
$scope.getUsersFromLocal = function() {
$http.get('http://localhost:8080/people').
success(function(data) {
$scope.data = data;
});
};
$scope.getUsersFromLocal(); //call ajax method
});
这篇关于Angularjs $ http.get不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!