0
我試圖獲取對象內數組的字段值的總數,並使用ng-repeat將其顯示爲視圖。對於每個名稱,我希望如何使用下面的JSON數據來顯示評論評分值的總和。我能夠獲得每個數組有多少評論的值,但我也無法顯示它。AngularJS顯示對象內數組字段值的總數
下面是我使用的JSON數據:
[{"_id":"56873fdb182b3741059357d1","longitude":123.76338959999998,
"latitude":10.2594266,"location":"Cebu City","name":"Cebu City","__v":0,
"category":"Attraction",
"reviews":[
{"comment":"This is a test. Changed to 3.5 stars.","rating":3.5,"userId":null,"review_id":"tiGCG2rGroIn"}
]},
{"_id":"56873fc9182b3741059357d0","longitude":113.83507800000007,"latitude":22.1533884,"location":"Hong Kong","name":"Hong Kong","__v":0,"category":"Attraction",
"reviews":[{"comment":"Add the comment to the form","rating":3.5,"userId":11,"review_id":"44NL7kkwhy72"},
{"comment":"I'm not impressed by your performance. - GSP","rating":1.5,"userId":11,"review_id":"jN7f1iFlQVha"},
{"comment":"Test","rating":2.5,"userId":11,"review_id":"QcJpw4yopF1q"}]}]
角碼
.controller('HomeCtrl', function($rootScope, $scope, LocFac) {
LocFac.getLocations().then(function successCallback(data) {
$scope.locations = data.data;
//for each location get a rating
$scope.loclen = ($scope.locations).length;
//for each repeat
var total = 0;
for(var i = 0; i < $scope.loclen; i++){
var loc = $scope.locations[i];
//get amount of reviews for each location
reviewlen = (loc.reviews).length;
locreviews = $scope.locations[i].reviews;
//average the reviews for each location
ratings = locreviews;
total += (ratings.rating);
console.log(ratings);
}
});
})
您想在哪裏顯示它?只有控制檯? –
@JohnEphraimTugado我想在視圖上顯示它。 –