サービスを使用して非同期httpコールを作成する単純なコントローラを設定しようとしています。私は書いた別のコントローラーと非常によく似たものをセットアップしましたが、何らかの理由でこれが機能していません。 console.log($ scope.art); then()ブロックの行は正しいデータを出力しますが、then()ブロックの外に出ると$ scope.artは未定義です。これはなぜでしょうか?私は非常に混乱しています!
app.controller('FeedbackController', ['$scope','getArt', function($scope, getArt) {
var art = getArt();
art.then(function(result) {
$scope.art = result;
console.log($scope.art);
}, function(reason) {
$scope.error = reason;
});
console.log($scope.art);
}])
.factory('getArt', ['$http', '$q', function($http, $q) {
return function() {
var deferred = $q.defer();
$http.post("/php/getArt.php")
.success(function (response) {
if (response == "nope") {
deferred.reject("Whoopsie! Something seems to have gone wrong.");
} else {
deferred.resolve(response);
}
})
.error(function() {
deferred.reject("There seems to be an issue with your connection.");
});
return deferred.promise;
};
}]);
ありがとうございます!
ああうわあ、私は馬鹿だルイーズ非同期(async)JavaScriptを説明する記事です!それはたくさんの意味があります。ありがとうございました! –
Wellcome to javascript&async @CarterScottDavis :) –