When utilizing a function to include an item, it is possible to track the last inserted ID by setting a variable within the process of adding the item to the list.
To illustrate this concept, refer to the following example: http://jsfiddle.net/DotDotDot/Eb2kR/
In this case, a variable named lastInsertedId
is created and utilized to assign a class in the ng-repeat block:
<span ng-class='{lastinserted: item.ID==lastInsertedId}'>{{ item.ID }} - {{ item.heading }} - {{ item.date | date:"dd.MM.yy" }}</span>
This implementation assumes that the IDs are unique numerical values, however, the approach can be adapted for different data sets as well.
$scope.addItem=function(){
var dd=new Date($scope.itemDate);
$scope.items.push( {"ID":$scope.items.length+1, "heading":$scope.itemName, "date":dd.getTime()});
$scope.lastInsertedId=$scope.items.length;
}
By updating the last inserted ID in this manner, the selected class will be applied to the respective item.
Additionally, the lastInsertedId
value can be unset within the delItem()
method if necessary.
In scenarios where a more complex logic is involved (such as non-sequential or non-numeric IDs), you can implement a custom function within the ng-class
. For instance, consider the modified implementation provided in this updated example: http://jsfiddle.net/DotDotDot/Eb2kR/2/:
$scope.amITheOne=function(item){
return item.ID==$scope.lastInsertedId;
}
Subsequently, utilize the function as follows:
<span ng-class='{lastinserted: amITheOne(item)}'>
While the concept remains fairly simple with basic logic, it can be extended to incorporate more intricate criteria based on various attributes like ID, name, and date.
Enjoy experimenting with these possibilities!