【发布时间】: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