配列の各オブジェクトをステップ実行するために必要なJSONオブジェクトがあります。私は現在、すべてのアイテムを表示することができますが、ロード時に最初のアイテムのみを表示できる必要があります。ボタンをクリックすると、配列内の次のオブジェクトに反復処理を行い、次のオブジェクトだけを表示する必要があります。この機能の例としてAngularでJSONオブジェクトをステップ実行
:http://jsbin.com/veyegihogi/edit?html,js,output
現在の設定:
のJavaScript:
var app = angular.module('app', []);
app.controller('portfolioController', function($scope, $http) {
$scope.indexToShow = 0;
$http.get("shows.json")
.then(function(response) {
console.log(response);
$scope.results = response.data;
});
$scope.change = function(){
$scope.indexToShow = ($scope.indexToShow + 1) % $scope.results.length;
};
});
マークアップ:
<div id="images" ng-repeat="items in results">
<div class="col-lg-2 col-md-2 col-sm-2">
<img src="{{items.image_url}}" alt="{{items.title}}">
</div>
</div>
<div class="simple-button" ng-click="change()">click me!</div>
サンプルデータ:
[
{
"id": 1,
"title": "title 1",
"count": 14,
"image_url": "images/image.jpg"
},
{
"id": 2,
"title": "title 2",
"count": 10,
"image_url": "images/image2.jpg"
},
{
"id": 3,
"title": "title 3",
"count": 8,
"image_url": "images/image3.jpg"
}
]
私は 'NG-click'が表示されませんあなたのマークアップ?また、 'product_image_url'はオブジェクト' image_url'と一致しませんさらに...あなたの '$ http.get()'の中に '$ scope.change'を持つ理由はありません –
また、ng-repeatは本当にちょうどクリックで次のものを表示すると仮定していますか? – defaultcheckbox
'src =" {{items [indexToShow] .product_image_url}} "' 'ng-repeatを削除します。それは明らかです!あなたに混乱しているのは何ですか? – sabithpocker