2

I'm having two JSON Array, $scope.data has the primary details, which I want to show in the UI, in that cInstId is the foreign key from the JSON $scope.color.

The two JSON Arrays are

$scope.data = [
    {
        "id": 1,
        "cTitle": "One",
        "cInstId": 1
    },
    {
        "id": 2,
        "cTitle": "Two",
        "cInstId": 2
    },
    {
        "id": 3,
        "cTitle": "Three",
        "cInstId": 3
    },
    {
        "id": 4,
        "cTitle": "Four",
        "cInstId": 4
    },
    {
        "id": 5,
        "cTitle": "Five",
        "cInstId": 4
    }
];

$scope.color = [
    {
        "cInstId": 1,
        "cInstTitle": "Blue"
    },
    {
        "cInstId": 2,
        "cInstTitle": "Green"
    },
    {
        "cInstId": 3,
        "cInstTitle": "Red"
    },
    {
        "cInstId": 4,
        "cInstTitle": "Orange"
    },
    {
        "cInstId": 5,
        "cInstTitle": "Violet"
    }
];

My expected output should be as like the picture

enter image description here

<div ng-repeat="members in data">
    <!-- How to Show it in the UI -->
</div>

Note: Don't create any temp array and for-each implementation in the Controller.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130

1 Answers1

0

You can use custom filter here for this.

angular.module('ModuleName').filter('color', function($filter) {
  return function(id, colorArray) {
    return $filter('filter')(colorArray,{'cInstId': id})[0].cInstTitle;
  };
});

Use this filter as follows:

<div ng-repeat="members in data">
    <!-- How to Show it in the UI -->
    <span>{{member.cInstId | color:color}}</span><!--Second color is your scope var -->
</div>

Note: I have used this and this

But it will be better if you can create your color service and use that service instated of using the array directly.

Community
  • 1
  • 1
Partha Sarathi Ghosh
  • 10,936
  • 20
  • 59
  • 84