Mohamed Sarre
Mohamed Sarre

Reputation: 17

Get the previous element in a scope

I am working on an exercice that makes me apply functions to elements (dynamically added) of a scope. The functions apply to an item I performed a ng-click on. Now, I have this :

console.log($scope.item)

What I would like to do is a print of the item that comes right before. The previous one. Is there some angular directive that helps retrieving it ? So far, i've tried :

console.log($scope.previous.item)
console.log($scope.item.prev)
console.log($scope.item[$index -1])

None seems to be okay.

Upvotes: 0

Views: 80

Answers (1)

Praveen
Praveen

Reputation: 347

If you want to know what previous value in a scope variable is best to use $watch, where you can access the new and old values as parameters like below

$scope.$watch('yourVariable',function(newValue, oldValue){
//do your stuff
});

Upvotes: 1

Related Questions