0

Say I have the following JSON file

{
  "A": [
    {"name": "foo",
      "BRef": "123"
    },
    {"name": "Hello",
      "BRef": "456"
    }
  ],

  "B": [
    {"ID": "123",
      "lastName": "bar"
    },
    {
      "ID": "456",
      "lastName": "World"
    }
    ]
}

I have linked the model and view via the controller

$http.get('js/data.json')
    .success(function(data) {
      $scope.A = data.A;
      $scope.B = data.B;

How do I loop through A and dig out the lastname from B?

<div ng-repeat="a in A">
 <h1>{{a.name}} {{ <<(B.b.ID == a.BRef).lastname>> }}</h1>
</div>

Was thinking that an ng-repeat of B with an filter might do the trick.. but I'm not that skilled with angular filter (yet)

3 Answers 3

1

you can use function for getLast name or use nested ng-repeat and use ng-if directive for display match item.

$scope.getLastName = function(BRef){
     var lastName = "";
      angular.forEach($scope.data.B,function(b){
         if(b.ID == BRef)
         lastName = b.lastName;
     });
     return lastName;
 }

in your view use

 <div ng-repeat="a in A">
  <h1>{{a.name}} <span ng-bind="getLastName(a.BRef)"></span></h1>
</div>

var editer = angular.module('editer', []);
function myCtrl($scope) {
   $scope.data = {
  "A": [
    {"name": "foo",
      "BRef": "123"
    },
    {"name": "Hello",
      "BRef": "456"
    }
  ],

  "B": [
    {"ID": "123",
      "lastName": "bar"
    },
    {
      "ID": "456",
      "lastName": "World"
    }
    ]
}
   
   $scope.getLastName = function(BRef){
     var lastName = "";
      angular.forEach($scope.data.B,function(b){
         if(b.ID == BRef)
         lastName = b.lastName;
     });
     return lastName;
 }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="editer" ng-controller="myCtrl" class="container">
   <div ng-repeat="a in data.A">
     <h1>{{a.name}} <span ng-bind="getLastName(a.BRef)"></span></h1>
   </div>
  
  OR
  ----------------------------------
  
   <div ng-repeat="a in data.A">
     <h1>{{a.name}} <span ng-repeat="b in data.B" ng-if="b.ID == a.BRef">{{b.lastName}}</span></h1>
   </div>
 
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

I really like the last solution since it can be applied more easy if I were to get other parameters than lastname
1

From one of the post that was deleted I actually found an elegant solution by combinding it with the ng-show

<div ng-repeat="a in A">
<div ng-repeat="b in B" ng-show="b.ID == a.BRef">

 <h1>{{a.name}} {{b.lastname}}</h1>

</div>
</div>

Comments

0

I reckon SSH is right.

You can use filter method of javascript i.e

$scope.findCorrespondingLastName(bref) {
    $scope.B.filter(function(b) {
        return b.ID == bref;
    });
}

and in your html file use

 <div ng-repeat="a in A">
  <h1>{{a.name}} {{findCorrespondingLastName(a.BRef)[0]['lastName']}}</h1>
</div>

Also try to use controllerAs alias and put the variables on this of the controller. Using $scope is not recommended as per latest angular docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.