I'm trying to figure the best way to do this, given that there will be a lot of data and I don't want to have giant loops hogging up resources. I have a lot of regular javascript/jquery experience, but am new to Angular and want to do it with Angularjs best practices.
I have a list of items and users can "favorite" them. When viewing all items, I'd like to show the ones that favorited by marking them with an indicator. Both items and favorites are in separate tables and returned as JSON with different IDs.
JS:
$scope.favorites = [];//$http.get of JSON file
$scope.items = [];//$http.get of JSON file
$scope.isFavorite = function(itemId) {
var len = $scope.favorites. !== 'undefined' ? $scope.favorites.length : 0;
for(var i = 0; i < len; i++) {
if($scope.favorites[i].id === itemId) {
return true;
}
}
};
HTML:
<div ng-repeat="item in items">
<i ng-class="isFavorite(item.id) ? 'icon-bookmark' : 'icon-bookmark-empty'"></i>
</div>
I have a couple other checks for "isFavorite" so I'm trying to figure the best way to compare the 2 without checking every time in a loop. Any suggestions?