我有一個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