2

Let us consider I have an array:

    var friendRequests=[
      {
      id:"Xyz",
      requester:"person 1"
      },
      {
      id:"Xyz2",
      requester:"person 2"
      }]

this array iterates in the template using ng-repeat="friendRequest in friendRequests". Meanwhile I want to grab the profile of each requester to show it with each friendrequest in ng-repeat. I am new to angular so I can't figure that out.

5
  • Could you post some more of your code? Specifically where you are getting the value for friendRequests in your code. Ideally that would be your controller, your routes, or the service that you are using to obtain that data. Commented Mar 27, 2016 at 15:18
  • Not clear what part you aren't sure of. Will need to loop over data and make a request for each one...or restructure data on server so it sends what you want Commented Mar 27, 2016 at 15:18
  • @charlietfl yes I need to loop over for each one of the requester id and get the profile data too Commented Mar 27, 2016 at 15:19
  • If it is possible you could get the profile for all users beforehand (Depends on the number of requesters) Commented Mar 27, 2016 at 15:19
  • Again...what part aren't you clear on? Commented Mar 27, 2016 at 15:22

1 Answer 1

2

If i get your question right it is quite simple:

<ul>
    <li ng-repeat="friendRequest in friendRequests">{{friendRequest.requester }}</li>
</ul>

in each iteration the scope would be the object you defined (in this case friendRequest) and you can access its attributes like any javascript object.

UPDATE:

So i understand you wish to get the profile for each user and display it, what you need to do is to change the server response to include the profile for each user so it will look something like this:

    var friendRequests=[
      {
      id:"Xyz",
      requester:"person 1",
      profile: {name: 'user1 name' , avatar: 'http://some/url'}
      },
      {
      id:"Xyz2",
      requester:"person 2",
      profile: {name: 'user2 name', avatar: 'http://some/url'}
      }]

Then to display it:

     <ul>
        <li ng-repeat="friendRequest in friendRequests">
           <span>{{friendRequest.requester}}</span>
           <span>{{friendRequest.profile.name}}</span>
       </li>
    </ul>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I figured out that part now I need to grab the profile of each requester in a seperate request (friendRequest.requester) and display it in the same <li>
So you want to send another request for each user to get his profile?
That is a very bad idea, it means you'll send tens if not hundreds of requests to your server. Instead you better change the response returned from the server to include the profile for each user
I figured out a way, I added a relationship at server level and I am grabbing them all together.
I see you point, you try to make something like infinite scroll. Take a look at ngInfiniteScroll: sroze.github.io/ngInfiniteScroll I think it may helps you

Your Answer

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