Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing a class from an element inside a $watch with angular

Tags:

css

angularjs

I want to remove a class on an element from within a $watch. The class would be removed after a $timeout of a 10 seconds.

Code looks something like this:

Controller:

$scope.$watch('lastPrice', function(newVal, oldVal){
    if ( newVal > oldVal ) {
        $scope.lastTick = 'up';
    } else if ( newVal < oldVal ) {
        $scope.lastTick = 'down';
    } else {
        $scope.lastTick = 'none';
    }

    $scope.last = newVal;

    $timeout(function(){
        //remove $scope.lastTick class name from element here
    }, 10000)
});

View:

<span class="last" ng-class="lastTick">{{lastPrice}}</span>
like image 332
chovy Avatar asked Nov 23 '25 15:11

chovy


2 Answers

I would avoid using the delete operator here since it would mean that the lastTick could be accidentally inherited from the parent scope. Instead, I would recommend:

$timeout(function () { $scope.lastTick = undefined; }, 10000);

Demo: http://jsfiddle.net/9ebjt/1/

like image 138
musically_ut Avatar answered Nov 26 '25 05:11

musically_ut


Try delete operator:

  $timeout(function(){
        delete $scope.lastTick;
    }, 10000);

JSFiddle: http://jsfiddle.net/cherniv/9ebjt/

like image 45
Ivan Chernykh Avatar answered Nov 26 '25 06:11

Ivan Chernykh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!