简体   繁体   English

单击关闭按钮后,启动警报不会关闭

[英]Bootstrap alert doesn't close on clicking close button

I am trying to use a bootstrap alert to show a warning. 我正在尝试使用引导警报来显示警告。 The alert fades and dismisses after some time but i would also like to give the user an option of closing it himself. 警报消失并在一段时间后关闭,但是我也想给用户一个关闭警报的选项。 I have added jquery and js/bootstrap.min.js in that order as has been suggested in answers to similar questions. 我已经按照类似问题的答案中的建议顺序添加了jquery和js / bootstrap.min.js。

This is my html code: 这是我的html代码:

<div class="alert alert-warning alert-dismissible errormessage" role="alert"
style="display: none;">
    <button type="button" class="close" data-dismiss="alert"
    ng-click="dismissError()">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
    </button>
    {{ errormessage }}
</div>

And this is my js: 这是我的js:

//sets errormessage to be displayed
function displayError(error) {
    if(error){
            $scope.errormessage = error;
            $('.errormessage').fadeIn(300).delay(5000).fadeOut(500);
    }

$scope.dismissError = function() {
    $scope.errormessage = '';
    $('.errormessage').hide();
};

CSS: CSS:

.errormessage{
    position: fixed;
    top: 1%;
    left: 50%;
    margin-left: -250px;
    width: 500px;
    padding-top: 5px;
    padding-bottom: 5px;
    z-index: 9999;
    text-align: center;
}

The implicit close didn't seem to be working, so i also tried the dismissError function to do it myself but on debugging i found that code flow never enters my function. 隐式关闭似乎没有用,所以我也尝试使用dismissError函数自己执行此操作,但是在调试时,我发现代码流从未进入我的函数。

NOTE: I have already tried doing 注意:我已经尝试过

<button type="button" class="close" data-dismiss="alert"
aria-hidden="true">&times;
</button>

and

<button type="button" class="close" data-dismiss="alert"
onclick="$('.alert').hide()" aria-hidden="true">&times;
</button>

both of which had no effect. 两者均无效。 Also I tried making the z-index to 2 but still cannot close the alert. 我也尝试将z-index设置为2,但仍然无法关闭警报。 Thanks for all the help! 感谢您的所有帮助!

PS: I am using this in a chromeapp that I am developing PS:我正在开发的chromeapp中使用它

Since you are using AngularJs with Bootstrap, why not use Angular UI for Bootstrap ? 由于您将AngularJs与Bootstrap一起使用,为什么不对引导程序使用Angular UI

You can add a ui-bootstrap-min.js file to your scripts and add a controller for the alert as below. 您可以将ui-bootstrap-min.js文件添加到脚本中,并为警报添加控制器,如下所示。 The HTML will be: HTML将是:

<div class="alert alert-warning alert-dismissible errormessage" role="alert" style="display: none;" ng-controller="AlertCtrl">  
         <alert type="{{alert.type}}" close="closeAlert()">{{ errormessage }}</alert>  
</div>

The javascript will be: JavaScript将是:

angular.module('ui.bootstrap.demo').controller('AlertCtrl', function ($scope) {  
    $scope.alert = [  
        { type: 'danger', errormessage: 'Your own error message' }, 
    ];  

    $scope.closeAlert = function(index) {
        $scope.alerts.splice(index, 1);
    };
 });

This is not a great Javascript function, but you should be able to build on it to get the solution. 这不是一个很棒的Javascript函数,但是您应该能够在此基础上获得解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM