0

I have two objects:

$scope.messages = {'to' => '1'};
$scope.users = {500 : 'id' => '1', 'name' => 'Dana' };

And ng-repeat in HTML template:

<div class="user-item" ng-repeat="message in messages">
{{users[{{messages.to}}].name}}
</div>

This part of code does not work:

{{users[{{messages.to}}].name}}

3 Answers 3

3

Since you're iterating over messages and in each iteration you have the message object, the correct code would be:

{{users[message.to].name}}

Also, I'm not really sure about your users object as the code above will look for users[1] and I don't see that in your example. Make sure it exists, if not, remap the users object into an array of objects where each index would be user's id.


Here's a simple example (note I had to fix/restructure the object from your PHP(?) notation):

angular.module('app', [])
.controller('Ctrl', function($scope){
  $scope.messages = [{'to': '1'}, {'to': '1'}, {'to': '2'}, {'to': '0'}, {'to': '3'}];
  $scope.users = [
   {'name': 'Dana' },
   {'name': 'Sana' },
   {'name': 'Xana' },
   {'name': 'Lana' }
 ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Ctrl">
  <div class="user-item" ng-repeat="message in messages">
    {{users[message.to].name}}
  </div>
</div>

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

Comments

2

You're trying to evaluate something from inside {{}} already (evaluate inside something already evaluating.

As an aside, you should also include any errors that you're getting and what exactly doesn't work. If you hit F12 in Chrome, you can see the Javascript console. That information would be helpful for use trying to resolve your problem.

I don't see why you're using ng-repeat on something holding only one value, and then you're using the plural in code.

Surely, you would want

{{users[message.to].name}}

rather than messages as you're pulling out one message item for each of the messages in that variable.

Comments

2

The double curly braces signify that something should be interpreted as an angular expression. There is no need to nest them. So since you trying to get $scope.users[messages.to].name just surround that expression as it would be in javascript (without the $scope) with the double curlies.

{{users[messages.to].name}}

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.