我无法从我的工厂 jsonLanguage
获取此 json 调用。
我的代码:
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
,但没有成功。
最佳答案
在这里你错过了几件事:
返回
$http
服务 promiseget method
get: function(theLanguage){ return $http.get(url); }
调用工厂
get method
& 通过将.then
放入其 promise 成功中获取数据通过该方法调用。jsonLanguage.get($scope.language).then(function(res){ $scope.language = res.data; });
关于javascript - 工厂方法 HTTP 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38794938/