I would like to have button or link with icon, default glyphicon-play or 
glyphicon-pause if interval is enabled. How can I refactor this directive 
especially $element.hasClass("glyphicon-pause") or 
$element.removeClass("glyphicon-pause").addClass("glyphicon-play"); in more 
"angular way"? 
I would be very grateful if you can help.

<button play class="btn glyphicon glyphicon-play"></button>

app.directive('play', ['$interval', function ($interval) {
    return {
        restrict: 'A',
        link: function ($scope, $element, attrs) {
            var i = 0,
                interval;


            var play = function () {
                $interval.cancel(interval);
                interval = $interval(function () {
                    $scope.states[i].active = false;
                    $scope.states[i++].active = true;
                    i = i % 3;
                }, 1000);
            };


            var stop = function () {
                $interval.cancel(interval);
            };
            console.log($element, attrs);


            $element.on('click', function ($event) {
                if ($element.hasClass("glyphicon-pause")) {
                    $element.removeClass("glyphicon-pause").addClass(
"glyphicon-play");
                    stop();
                } else {
                    $element.removeClass("glyphicon-play").addClass(
"glyphicon-pause");
                    play();
                }
            });
        }
    };
}]);

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to