Let's say I have two json objects, one of the users and one of the accounts, something like:
users = [ {id: 1, account_id: 1}, {id: 2, account_id: 2}, {id: 3, account_id: 1} ] accounts = [ {id: 1, name: "administrator"}, {id: 2, name: "moderator"} ]
Thus, I need to iterate over all arrays of users and each of them gets account information. What is the best way to manage these relationships to access them in markup? I found the following solutions:
Approach 1: Repeat only one element, so that it simply filters out the elements and makes them available in this part of the markup
<div ng-repeat="user in users"> <div ng-repeat="account in getAccountFromId(user.account_id)"> </div> </div>
Approach 2: change the way information is returned from the backend, resulting in a json for each user, where each json is returned with account information. But this will repeat a lot of information in every json object. This also implies a change in the backend due to angular.
<div ng-repeat="user in users"> </div>
Can someone tell me if these approaches are correct? Is there a better way to manage object relationships in angular?
Thank you very much.
source share