I have 2 (array of) objects from 2 different controllers.
Object 1 (Name Controller 1):
[{id:1,"name":"jakov"},...]
Object 2 (Nickname Controller 2 ):
[{id:1, "nickname" : "jandric", "nameId" :1, "title" : "master"},...]
I broadcast object from controller 1 via service:
$http.get('names/').success(function(data) {
sharedService.broadcastItem(data);
});
I handle broadcasted object in Controller 2:
$scope.$on('handleBroadcast', function() {
$scope.names= sharedService.message;
});
Controller 2 allso have GET request:
$http.get('nicknames/').success(function (data) {
$scope.nicknames = data;
});
I have simple table in which i want to show: nicknameId, nickname, name, and title.
Something like this:
<div ng-controller="Nickname">
<table>
<tr>
<th>nicknameId</th>
<th>nickname</th>
<th>name</th>
<th>title</th>
</tr>
<tr ng-repeat="nick in nicknames">
<td>{{nick.id}}</td>
<td>{{nick.nickname}}</td>
<td ng-controller="Name">{{??????}}</td> //GET name request trigger
<td>{{nick.title}}</td>
</tr>
</table>
</div>
How to get name from first object, for given nameId, in second object??
Plz help :D
UPDATE
I managed to do it in html like this:
<div ng-controller="Nickname">
<table>
<tr>
<th>nicknameId</th>
<th>nickname</th>
<th>name</th>
<th>title</th>
</tr>
<tr ng-repeat="nick in nicknames">
<td>{{nick.id}}</td>
<td>{{nick.nickname}}</td>
<td ng-controller="Name">
<div ng-repeat="name in names">
<div ng-if="nick.nameId === name.id">
{{name.name}}
</div>
</div>
</td>
<td>{{nick.title}}</td>
</tr>
</table>
</div>
So i'm using for loop inside for loop. Is there a more simple answer?
I hope I got your question right: create an index in the Nickname controllers $scope which maps to the name by the id:
then you can access the index in the template: