【发布时间】:2015-08-26 16:38:32
【问题描述】:
我正在编写一个调用 JSON 提要并返回结果的工厂。
这里是使用 $http 的工厂
nearestLocationApp.factory("allTheLocationsFactory", function($http){
var locations = "Not sure why it don't work";
$http.get('/json/locations/').then(function(res){
locations = res.data;
});
return locations;
});
当我将它包含在控制器中时,我得到了位置变量的第一个版本。就像这里有2个不同的范围。我很确定这是我的问题,但我不知道为什么。
这是我调用并打印出控制器的 console.log。
nearestLocationApp.controller("getTheLocation", function($scope, allTheLocationsFactory){
$scope.locationResponse = "Find your location";
$scope.locations = allTheLocationsFactory;
console.log($scope.locations);
});
我需要知道的是为什么我的工厂有多个示波器以及如何修复它。
如果有人感兴趣,JSON 数据如下所示。
[
{
"Name":"#########",
"ID":#########,
"address1":"#########",
"address2":"#########",
"city":"#########",
"state":"#########",
"zip":"#########",
"phoneNumber":"#########",
"thumbnail":[
"#########",
#########,
#########,
#########
],
"permalink":"#########",
"weekdayHours":"#########",
"saturdayHours":"#########",
"sundayHours":"#########",
"coords":{
"longitude":"#########",
"latitude":"#########"
},
"options":{"animation":#########}
}, ......
【问题讨论】:
标签: json angularjs rest angularjs-scope