HTTP要求から別のスコープオブジェクトに戻りオブジェクトを取得したいので、ng-repeat
ディレクティブのHTMLで使用できます。返されたオブジェクトを変数に取得するにはどうすればよいですか?
- コールHTTP機能
- HTTPレスポンスの値
JSスコープ変数を作成し、データ
angular.module('myApp').controller('ItemController',[
'$scope', '$http', '$location',
function($scope, $http, $location){
var currentLocation = $location.path();
$scope.getItem = function(){
$http.get('path/to/json/file.json').then(
function(response){
$scope.results = response.data;
console.log('item controller, item:', $scope.results);
});
};
$scope.item = //MAKE THIS VALUE OF THE RETURN OBJECT FROM getItem
console.log('scope.item', $scope.item);
}
]);
JSON
[
{
"first_name": "Mega",
"last_name": "Man",
"image": "http://placehold.it/75x75"
}
]
HTML
がちょうどJSコール
$scope.getItem = function(){
$http.get('path/to/json/file.json')
.success(function(data){
$scope.results=data;
console.log('$scope.results: ',$scope.results);
}).error(function(data){
console.log(data);
}).then(
function(){
$scope.item = $scope.results;
console.log('$scope.item: ', $scope.results);
});
};
で、配列の最初のオブジェクトを取得するために、成功コールバックにありました彼らがドライブウェイを出る前のテーブルの上に。 '$ scope.item'をコールバック内に設定する必要があります。 –
これで、JS呼び出しを更新し、 '.success()'メソッドからの初期呼び出しで '.then()'メソッドからデータを取得して実行しました。 HTMLにどのように公開されるのでしょうか? –
iircとまったく同じです。 .thenと$ scope.resultsは冗長ですが。私は 'データ'は、あなたが思うような構造を持っていないと思う。 –