0

I have an Object that I'm iterating over, that contains array values, here's an example (Note that my version has a lot more information than just content):

$scope.messages = { "1": [ 
    { "content": "Hello" },
    { "content": "How are you" },
    { "content": "This should be displayed" }
]};

Take this as a "messaging thread" type of scenario, where you would group the threads by a phone number (in this event, 1) and then show the last received message (the last index of the array).

Here's what I've tried, but it doesn't work.

<div ng-repeat="(thread_id, message_data) in messages">
    Thread ID: {{thread_id}} <br>
    Last message: {{message_data[message_data.length - 1].content}}
</div>

The thread id shows, but the message data does not.

Hardcoding the value such as:

{{message_data[0].content}}

works as expected.

Before people start suggesting $last please note that $last only returns true or false, depending on if the ng-repeat iteration is on its last cycle. Considering I'm using a (key, value) pair, and in my case the array is the value. I need the array length of the value, which value.length does provide. In this event:

 {{ message_data.length }}

will print out the value of 3. However using message_data.length - 1 to obtain a value from the array does not work.

4
  • @Trushnar - The $last variable returns a boolean, it says so straight on the document page. I need to get the length of the array in the (key, value) structure. Where the array is the value.. $last only returns true or false if the current ng-repeat iteration is on the last index. (In this case, it's the index of threads, not the index of messages) Commented Oct 9, 2015 at 7:10
  • works perfectly here jsfiddle.net/0jatojvg you have some internal issue, or given information is not enough to see the issue Commented Oct 9, 2015 at 7:18
  • @GuramiDagundaridze - Oh joyous, aren't those the best! I just got it to work due to a suggestion by a friend. Which was to change the code to message_data[(message_data.length - 1)].content and apparently the parenthesis solved the issue. Commented Oct 9, 2015 at 7:19
  • 1
    i am still curious because in fiddle it works without parenthesis. Must be a version difference thing Commented Oct 9, 2015 at 7:20

2 Answers 2

1

Add this to your html

ng-init="$last ? getLastIndex($index) : ''"

Js

$scope.getLastIndex = function (index){
  $scope.lastIndex = index;
}

Then this should work fine

message_data[lastIndex].content
Sign up to request clarification or add additional context in comments.

Comments

0

While my exact scenario may not be present on all versions of AngularJS, I solved the issue by surrounding my math in parenthesis.

message_data[(message_data.length - 1)].content

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.