I think it would be better to observer a concrete value per highlighter instead of watching the whole collection. E.g.:
<td highlighter="person.firstName">{{ person.firstName }}</td>
This way, the highlighter
-directive could be very simple, like:
app.directive('highlighter', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
model: '=highlighter'
},
link: function(scope, element) {
scope.$watch('model', function (nv, ov) {
if (nv !== ov) {
// apply class
element.addClass('highlight');
// auto remove after some delay
$timeout(function () {
element.removeClass('highlight');
}, 1000);
}
});
}
};
}]);
Though, for this to work you'll have to tell angular that the data actually changed. Currently this is not the case as angular tracks people
by object-identity. The moment you overwrite it, angular will remove all associated dom-elements. To accomodate for this, use:
ng-repeat="person in people track by $index"
which will tell angular to treat the index of the array as identity.
demo: http://jsbin.com/vutevifadi/1/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…