gpt4 book ai didi

javascript - angularjs从url获取数据

转载 作者:行者123 更新时间:2023-11-28 15:24:16 25 4
gpt4 key购买 nike

我想从 url 获取 json 数据并将其分配给一个变量。我使用的服务看起来像这样

app.service('dataService', function($http) {
this.getdata = function(callbackFunc) {
$http({
method: 'GET',
url: '/records/json/',
}).success(function(data){
// With the data succesfully returned, call our callback
callbackFunc(data);
}).error(function(){
alert("error");
});
}
});

我的 Controller 看起来像这样

app.controller('ReCtrl', ['$scope', function($scope, dataService){ 
dataService.getdata(function(dataResponse) {
$scope.fields = dataResponse;
});
....

但我收到错误TypeError:无法读取未定义的属性“getdata”。我不知道我做错了什么。我感谢任何帮助。

最佳答案

您没有将服务 (dataService) 注入(inject) Controller :

app.controller('ReCtrl', 
['$scope', 'dataService', function($scope, dataService){
dataService.getdata(function(dataResponse) {
$scope.fields = dataResponse;
});
....

在定义 Controller ReCtrl 时,请注意 '$scope' 之后的额外字符串 'dataService'

关于javascript - angularjs从url获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29798946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com