我有一个控制器,可以从用户那里获取信息并为他找到完美的水果。如果在我的json文件中没有水果的描述,它将从维基百科(wikimedia api)获得。
问题是我无法将承诺附加到描述变量。
我会赞美它,你可以看看,
由于
app.controller('fruitsCtrl', ['$scope', '$http', 'preferences', function ($scope, $http, preferences) {
$scope.preferences = preferences; //what kind of fruits preferences the user have
// local json files that has info about certain fruits
$http.get("partials/fruits.json").then(function(response) {
$scope.data = response.data; // Question -> is this good practice???
$scope.fruits = {};
// look at json file for fruits that correspond the preferences
for (i = 0; i < $scope.preferences.length; i++) {
for (l = 0; l < $scope.data.length; l++) {
if($scope.data[l].fruitProperties.indexOf($scope.preferences[i]) > -1){
// add this fruit details to the fruits object
$scope.fruits[l] = $scope.data[l];
// if description of fruit is not in json file it
// will have a var - "wikiname" to get it from wikimedia API
if ($scope.fruits[l].description === undefined){
var wiki = $scope.fruits[l].wikiName;
// with wikimedia I can use only $http and not $http.get
$http({
url: $scope.url = "https://en.wikipedia.org/w/api.php?format=json&callback=JSON_CALLBACK&action=query&prop=extracts&exintro=true&titles="+wiki,
method: 'jsonp'
}).success(function(response) {
for(var id in response.query.pages) {
$scope.fruits[l].description = response.query.pages[id].extract;
}
});
}
}
}
}
}, function () {
$scope.sites = [{action: "Error"}] //add somthing in case of error
});
}]);
答案 0 :(得分:0)
我建议将get功能放入服务或工厂,但它可以在控制器内部工作。
我建议采用两部分方法。使用$ templateRequest访问您的JSON,然后如果没有数据使用$ http执行对Wiki的调用。
对于未定义的错误,我假设您正在尝试将其分配给对象是吗?如果是这样,请在分配之前尝试将其实例化为对象。
YourVarName.prop = {};
YourVarName.prop = response;
抱歉,它只是单击了整个对象,而不仅仅是新属性未定义。以上不起作用。
您是否考虑过在成功函数中使用回调函数?
//Inside success
callback(response, l);
//End success
function callback (response, l) {
$scope.yourproperties[l] = response;
}
通过将分配移出成功,您可以解决导致其未定义的问题。
答案 1 :(得分:0)
我就是这样解决的:
app.controller('fruitsCtrl', ['$scope', '$http', 'preferences', function ($scope, $http, preferences) {
$scope.preferences = preferences; //what kind of fruits preferences the user have
// local json files that has info about certain fruits
$http.get("partials/fruits.json").then(function(response) {
$scope.data = response.data; // Question -> is this good practice???
$scope.fruits = {};
// look at json file for fruits that correspond the preferences
for (i = 0; i < $scope.preferences.length; i++) {
for (l = 0; l < $scope.data.length; l++) {
if($scope.data[l].fruitProperties.indexOf($scope.preferences[i]) > -1){
// add this fruit details to the fruits object
$scope.fruits[l] = $scope.data[l];
getMissingFruitsDescriptionFromWiki($scope.fruits, l);
}
}
}
}
}, function () {
$scope.sites = [{action: "Error"}] //add somthing in case of error
});
function getMissingFruitsDescriptionFromWiki (fruits, l) {
// if description of fruit is not in json file it
// will have a var - "wikiname" to get it from wikimedia API
if ($scope.fruits[l].description === undefined){
var wiki = $scope.fruits[l].wikiName;
// with wikimedia I can use only $http and not $http.get
$http.jsonp("https://en.wikipedia.org/w/api.php?format=json&callback=JSON_CALLBACK&action=query&prop=extracts&exintro=true&titles=""https://en.wikipedia.org/w/api.php?format=json&callback=JSON_CALLBACK&action=query&prop=extracts&exintro=true&titles="+wiki).success(function(response) {
for(var id in response.query.pages) {
$scope.fruits[l].description = response.query.pages[id].extract;
}
});
}
}]);