Question:
Is it possible to define an ng-repeat track by expression which will use a particular attribute if it exists, but use the default $$hashKey behavior if that attribute is undefined?
Context:
I have a chat application which displays an array of JSON "post" objects which have id's. Typically, I would do
<div ng-repeat="post in posts track by post.id">{{post.content}}</div>
However, since I want the app to feel responsive to the user, when a user adds a new chat, I immediately push it onto the array BEFORE I make the create API call. I then update the id if the API response was successful, or update a "failed" field if the API response was not successful.
This means it is possible for the posts
array to have multiple post objects which have value undefined
for id if there were failed posts.
So I'm hoping it's possible to still use $$hashKey when there is no id defined.