【发布时间】:2016-12-04 21:50:18
【问题描述】:
我想从控制器“menuBarCtrl”调用 Angular 服务函数“getDecks()”。在 getDecks() 返回我的 JSON 文件的内容后,我想将文件的内容存储在本地 $scope 变量中。
但是,如果我尝试在控制台中显示该值,我只会得到一个“未定义”。
ajapp.controller('MenuBarCtrl', function($scope, $http, CardProvider) {
$scope.decks = CardProvider.getDecks();
console.log($scope.decks);
});
ajapp.service('CardProvider', function($http) {
this.getDecks = function(){
var json;
$http.get('content/SetList.json').success(function(data, status){
json = data;
}).error(function (data, status) {
alert("File Request Failed ["+status+"]");
});
return json;
}
});
【问题讨论】:
-
在
$http.get函数中尝试return语句。 -
你知道 Promise 是如何工作的吗...?
标签: javascript angularjs json