对于$ timeout,AngularJS似乎无法使用可变持续时间

时间:2013-09-17 12:05:41

标签: angularjs angularjs-controller angularjs-timeout

我正在尝试在控制器中实现一个功能,该功能将向用户显示各种通知。

问题是我希望持续时间是一个函数参数,而这似乎不起作用。

怎么回事?。

我该如何解决这个问题?。

    $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);
   }

1 个答案:

答案 0 :(得分:1)

试试这个

$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