Oleg Belousov
Oleg Belousov

Reputation: 10121

AngularJS cannot seem to take a variable duration for a $timeout

I am trying to implement a function within the controller which will be incharge of showing various notification to the user.

the problem is that I want the duration to be a function parameter, and that doesn't seem to work.

How come?.

How can I fix this?.

    $scope.layout.showNotification = function(msg, duration){
            $scope.layout.notification.message = msg;
            $scope.layout.notification.visible = true;

            if(!duration || duration === null)
                return

            $timeout(function(){
                $scope.layout.notification.visible = false;
                $scope.layout.notification.message = "";
            }, duration);
   }

Upvotes: 0

Views: 603

Answers (1)

Nitish Kumar
Nitish Kumar

Reputation: 4870

Try This

$scope.notification = {
    message : '',
    visible: true
};
$scope.showNotification = function(msg, duration) {
        $scope.notification.message = msg;
        $scope.notification.visible = true;

        if(!duration || duration === null)
            return

        $timeout(function(){
            $scope.notification.visible = false;
            $scope.notification.message = "";
        }, duration);
};

$scope.showNotification('MSH',5000);

DEMO

Upvotes: 1

Related Questions