工厂方法HTTP不起作用 [英] Factory method HTTP not working
本文介绍了工厂方法HTTP不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法从我的工厂 jsonLanguage
获得此json调用.
I cannot get this json call from my factory jsonLanguage
.
我的代码:
var App = angular.module('App', []);
var theLanguage = 'english';
App.factory('jsonLanguage', function($http){
var theLanguage = 'english';
return {
get: function(theLanguage){
var url = theLanguage + '.json';
$http.get(url);
}
}
});
App.controller('mainController', function($scope, $http, $log, jsonLanguage) {
//$http.get(theLanguage + '.json')
jsonLanguage.success(function(res){ //here is the problem I tried .then and tried res.data no luck :(
$scope.language = res;
$log.debug($scope.language);
});
$log.debug($scope.language);
angular.forEach($scope.letter, function(single) {
$log.log("hello worldsss");
$log.log(single);
});
});
App.controller('intervalController', function($scope, $log) {
this.$log = $log;
//var name = $scope.single;
angular.forEach($scope.letter, function(single) {
$log.log("hello worldsss");
$log.log(single);
});
//$log.log(name);
$log.log('Hello World!');
});
App.controller('clickController', function($scope) {
});
我尝试了 jsonLanguage
,然后又尝试了 res.data
,但是没有运气.
I tried jsonLanguage
, and then and tried res.data
with no luck.
推荐答案
在这里您错过了几件事:
Here you missed couple of things:
-
从服务
get方法
get: function(theLanguage){
return $http.get(url);
}
调用工厂 get方法
&通过将 .then
置于该方法调用之上,可以在其成功承诺范围内获取数据.
Call factory get method
& get data inside its promise success by putting .then
over that method call.
jsonLanguage.get($scope.language).then(function(res){
$scope.language = res.data;
});
这篇关于工厂方法HTTP不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文