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.
$lastvariable 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 thevalue..$lastonly returns true or false if the currentng-repeatiteration is on the last index. (In this case, it's the index of threads, not the index of messages)message_data[(message_data.length - 1)].contentand apparently the parenthesis solved the issue.