AngularJS Bootstrap 警报关闭超时属性不起作用
AngularJS Bootstrap alert dismiss-on-timeout attribute doesn't work
我正在尝试使用 AngularJS Bootstrap 警报,如 here 所解释的 "dismiss-on-timeout" 属性。在此示例中没有任何效果,警报只是定期出现并且不会消失。
<alert type="warning" dismiss-on-timeout="2000">Alert text</alert>
但是它在网站的 ng-repeat 示例中确实有效:
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)" dismiss-on-timeout="2000">{{alert.msg}}</alert>
问题是缺少关闭属性吗?如果是这样,您如何为不属于数组的警报编写关闭函数?
很好,它只是 dismissOnTimeout
指令调用警报指令控制器的 close
method。该控制器依次使用外部作用域 close
方法。所以你需要用它来实现它,以便指令可以调用它:
<alert type="danger" close="closeAlert()" ng-if="show"
dismiss-on-timeout="2000">Something happened.</alert>
在控制器中:
$scope.show = true;
$scope.closeAlert = function(index) {
$scope.show = false;
};
这里的解决方案是正确的,但是已经过时了,所以这里是更新版本。
在视图中:(在 Angular UI Bootstrap 中更新)
<uib-alert type="{{alert.type}}" close="closeAlert()" dismiss-on-timeout="2000" ng-if="show">
{{alert.msg}}
</uib-alert>
在控制器中:
$scope.show = true;
$scope.closeAlert = function() {
$scope.show = false;
};
$scope.alert = {type: 'danger', msg: 'Something gone wrong'};
实际上,您不需要使用变量 ($scope.show = false;
) 来隐藏警报。您需要做的就是确保保存警报的数组一次只能有一项,除非您希望 multiple/previous 警报显示在屏幕上。只需在显示警报后将其删除即可。见下文:
标记
<uib-alert ng-repeat="alert in alerts" dismiss-on-timeout="2000" type="{{alert.type}}" close="closeAlert()">{{alert.msg}}</uib-alert>
控制器
//array to hold the alerts to be displayed on the page
$scope.alerts = [];
/**
*This function is used to push alerts onto the alerts array.
*/
$scope.addAlert = function(type, message) {
//add the new alert into the array of alerts to be displayed.
$scope.alerts.push({type: type, msg: message});
};
/**
*This function closes the alert
*/
$scope.closeAlert = function(index) {
//remove the alert from the array to avoid showing previous alerts
$scope.alerts.splice(0);
};
所以当你想显示警报时:
$scope.addAlert('success', 'My message');
我一直无法让它工作。这是一个更直接的方法:
标记
<div uib-alert
data-closeable="true" <!-- sets the 'x' on the right for closing -->
close="closeAlert()" <!-- what the 'x' and the timeout will call -->
alert alert-{{ alert.level }}" <!-- sets the color (e.g. 'success' or 'danger') -->
ng-show="alert.show"> <!-- only show me when this is truthy -->
{{ alert.message }}
</div>
控制器
$scope.closeAlert = function() {
$scope.alert.show = false;
}
$scope.showAlertForFiveSeconds = function(){
$scope.alert.message = 'Something went wrong!');
$scope.alert.level = 'danger'; // red
$timeout(closeAlert, 5000); // close the alert in 5 seconds
}
基本上我所做的就是手动设置延迟调用以关闭警报并走开。
请注意,这样做还需要您将 Angular $timeout 服务注入到顶部的控制器中。
我的建议是将它包装在一个可以在任何地方使用的 alertFactory
:
App.factory('alertFactory',['$rootScope',
function($rootScope) {
var alertService = {};
$rootScope.alerts = [];
// will automatidally close
// types are success, warning, info, danger
alertService.addAuto = function(type, msg, delay) {
var alert = {'type': type, 'msg': msg};
$rootScope.alerts.push(alert);
if (!delay ) {
delay = 2500; // default delay is 2500ms
}
window.setTimeout(function() {
var index = $rootScope.alerts.indexOf(alert);
if (index > -1) {
$rootScope.alerts.splice(index, 1);
$rootScope.$apply(); // refresh GUI
}
}, delay);
}
return alertService;
}])
放置这个,例如在你的`index.html
<script src="components/angular-ui-bootstrap-bower/ui-bootstrap-tpls.js"></script>
...
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert>
点赞
App.controller('myCtrl', [ "alertFactory", function(alertFactory) {
var optionalDelay = 2500;
alertFactory.addAuto('success', 'my alert text', optionalDelay);
}]);
我正在尝试使用 AngularJS Bootstrap 警报,如 here 所解释的 "dismiss-on-timeout" 属性。在此示例中没有任何效果,警报只是定期出现并且不会消失。
<alert type="warning" dismiss-on-timeout="2000">Alert text</alert>
但是它在网站的 ng-repeat 示例中确实有效:
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)" dismiss-on-timeout="2000">{{alert.msg}}</alert>
问题是缺少关闭属性吗?如果是这样,您如何为不属于数组的警报编写关闭函数?
很好,它只是 dismissOnTimeout
指令调用警报指令控制器的 close
method。该控制器依次使用外部作用域 close
方法。所以你需要用它来实现它,以便指令可以调用它:
<alert type="danger" close="closeAlert()" ng-if="show"
dismiss-on-timeout="2000">Something happened.</alert>
在控制器中:
$scope.show = true;
$scope.closeAlert = function(index) {
$scope.show = false;
};
这里的解决方案是正确的,但是已经过时了,所以这里是更新版本。
在视图中:(在 Angular UI Bootstrap 中更新)
<uib-alert type="{{alert.type}}" close="closeAlert()" dismiss-on-timeout="2000" ng-if="show">
{{alert.msg}}
</uib-alert>
在控制器中:
$scope.show = true;
$scope.closeAlert = function() {
$scope.show = false;
};
$scope.alert = {type: 'danger', msg: 'Something gone wrong'};
实际上,您不需要使用变量 ($scope.show = false;
) 来隐藏警报。您需要做的就是确保保存警报的数组一次只能有一项,除非您希望 multiple/previous 警报显示在屏幕上。只需在显示警报后将其删除即可。见下文:
标记
<uib-alert ng-repeat="alert in alerts" dismiss-on-timeout="2000" type="{{alert.type}}" close="closeAlert()">{{alert.msg}}</uib-alert>
控制器
//array to hold the alerts to be displayed on the page
$scope.alerts = [];
/**
*This function is used to push alerts onto the alerts array.
*/
$scope.addAlert = function(type, message) {
//add the new alert into the array of alerts to be displayed.
$scope.alerts.push({type: type, msg: message});
};
/**
*This function closes the alert
*/
$scope.closeAlert = function(index) {
//remove the alert from the array to avoid showing previous alerts
$scope.alerts.splice(0);
};
所以当你想显示警报时:
$scope.addAlert('success', 'My message');
我一直无法让它工作。这是一个更直接的方法:
标记
<div uib-alert
data-closeable="true" <!-- sets the 'x' on the right for closing -->
close="closeAlert()" <!-- what the 'x' and the timeout will call -->
alert alert-{{ alert.level }}" <!-- sets the color (e.g. 'success' or 'danger') -->
ng-show="alert.show"> <!-- only show me when this is truthy -->
{{ alert.message }}
</div>
控制器
$scope.closeAlert = function() {
$scope.alert.show = false;
}
$scope.showAlertForFiveSeconds = function(){
$scope.alert.message = 'Something went wrong!');
$scope.alert.level = 'danger'; // red
$timeout(closeAlert, 5000); // close the alert in 5 seconds
}
基本上我所做的就是手动设置延迟调用以关闭警报并走开。
请注意,这样做还需要您将 Angular $timeout 服务注入到顶部的控制器中。
我的建议是将它包装在一个可以在任何地方使用的 alertFactory
:
App.factory('alertFactory',['$rootScope',
function($rootScope) {
var alertService = {};
$rootScope.alerts = [];
// will automatidally close
// types are success, warning, info, danger
alertService.addAuto = function(type, msg, delay) {
var alert = {'type': type, 'msg': msg};
$rootScope.alerts.push(alert);
if (!delay ) {
delay = 2500; // default delay is 2500ms
}
window.setTimeout(function() {
var index = $rootScope.alerts.indexOf(alert);
if (index > -1) {
$rootScope.alerts.splice(index, 1);
$rootScope.$apply(); // refresh GUI
}
}, delay);
}
return alertService;
}])
放置这个,例如在你的`index.html
<script src="components/angular-ui-bootstrap-bower/ui-bootstrap-tpls.js"></script>
...
<alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</alert>
点赞
App.controller('myCtrl', [ "alertFactory", function(alertFactory) {
var optionalDelay = 2500;
alertFactory.addAuto('success', 'my alert text', optionalDelay);
}]);