Compare 2 GET object from different controllers

96 views Asked by At

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?

2

There are 2 answers

2
TekTimmy On BEST ANSWER

I hope I got your question right: create an index in the Nickname controllers $scope which maps to the name by the id:

/**
 *
 * @param {Array.<obj>} source: The array of objects the index should be created with.
 * @param {string} property: The property that shall be used as key for the new index
 * @return {object} provides access to all objects by the chosen property
 */
var buildIndex = function(source, property) {
    var temp = {};
    for(var i = 0, len = source.length; i < len; ++i) {
        temp[source[i][property]] = source[i];
    }
    return temp;
};

then you can access the index in the template:

<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>{{index[nick.id].name}}</td>
        <td>{{nick.title}}</td>
    </tr>
</table>

0
Rads On

Try 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">
                <span ng-reapeat="n in names">
                <span ng-if="nick.nameId == n.id">{{n.name}}</span>
                </span>
            </td> //GET name request trigger
            <td>{{nick.title}}</td>
        </tr>
    </table>
</div>