[英]Angular.ui alert doesn't close
I'm use the Angular-ui and i want to create single alert. 我使用Angular-ui,我想创建单一警报。 But Alert won't to close
但警报不会关闭
<div ng-controller="AlertCtrl">
<alert type="danger" close>i'm alert</alert>
</div>
实际上,如果您将消息放在html中,您甚至不需要控制器和函数:
<alert type="danger" close="bCloseAlert=1" ng-hide="bCloseAlert">i'm alert</alert>
If you only ever want to show one alert, you need to change your approach a little. 如果您只想显示一个警报,则需要稍微改变一下。
Your should pass a function from your AlertCtrl's scope to the "close" attribute, like this: 你应该将一个函数从AlertCtrl的范围传递给“close”属性,如下所示:
<alert type="{{alert.type}}" close="closeAlert()" ng-show="alert != null">
{{alert.msg}}
</alert>
Note that your type, and the text of the alert, now reference an alert object. 请注意,您的类型和警报文本现在引用警报对象。 Also note that I have added the ng-show attribute to only show the alert markup if the alert object is not null.
另请注意,我已添加ng-show属性,仅在警报对象不为空时才显示警报标记。
And then implement that function in your controller, and define an alert object for it to manipulate: 然后在您的控制器中实现该功能,并为其定义一个警报对象来操作:
$scope.alert = { type: 'danger', msg: 'im alert' };
$scope.closeAlert = function() {
$scope.alert = null;
}
You can use in your html: 你可以在你的html中使用:
<div ng-show="hasError">
<alert ng-repeat="error in errors track by $index" type="danger" close="closeAlert($index)">{{error}}</alert>
</div>
and in your controller: 并在您的控制器中:
$scope.hasError = false;
$scope.errors = [];
$scope.closeAlert = function (index) {
$scope.errors.splice(index, 1);
};
Then, you can close each alert window. 然后,您可以关闭每个警报窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.