【问题标题】:Angular Bootstrap alerts do not dismiss on a timeoutAngular Bootstrap 警报不会在超时时关闭
【发布时间】:2014-12-17 21:36:44
【问题描述】:

我正在尝试从 Angular Bootstrap 添加警报并在超时时自行关闭。然而,它并没有放弃自己。代码如下:

angular.module('ui.services').service('AlertService', [
  function () {
    var alerts = [];
    this.add = function(type, msg){
        var self = this;            
        return alerts.push({
            type: type,
            msg: msg,
            close: function() {
                return self.closeAlert(this);
            }
        });
        $timeout(function(){ 
            self.closeAlert(this); 
        }, 3000);

    },
    this.closeAlert = function(alert) {
        return this.closeAlertIdx(alerts.indexOf(alert));
    },
    this.closeAlertIdx = function(index) {
        return alerts.splice(index, 1);
    },
    this.alertData = function() {
        return alerts;
    }
}]);

我已经设置了超时,但不知道出了什么问题。

【问题讨论】:

  • 所以在 ControllerA $scope.alerts = AlertService.alertData();在控制器 B 中,我添加了要显示为 AlertService.add('success', "this is deleted", true); 的警报。所以我将 $scope .alerts 绑定到警报服务中定义的本地警报数组

标签: javascript angularjs twitter-bootstrap alerts


【解决方案1】:

您的$timeout 永远不会被调用,因为您刚刚返回。

否则,乍一看,一切似乎都是正确的。

【讨论】:

  • 所以在我在这里返回之前我不确定如何运行超时
  • 什么意思?把它放在之前返回
  • 是的,我明白了,但在这种情况下,警报甚至没有显示,因为警报没有被触发,因为在两个控制器之间使用的 $scope 属性稍后会使用来自的警报数组进行初始化警报服务电话
  • 我不确定我是否理解... $scope 属性是什么?您可能需要显示更多代码或创建一个新问题。
  • 所以在 ControllerA 我有 $scope.alerts = AlertService.alertData();在控制器 B 中,我添加了要显示为 AlertService.add('success', "this is deleted", true); 的警报。所以我将 $scope .alerts 绑定到警报服务中定义的本地警报数组 -
【解决方案2】:

https://github.com/angular-ui/bootstrap/blob/master/src/alert/alert.js#L22-L31

<alert dismiss-on-timeout='2000'>...</alert>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-08
    • 2014-04-10
    • 2015-07-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 2017-03-06
    相关资源
    最近更新 更多