单击关闭按钮时,Bootstrap警报不会关闭

时间:2021-01-18 21:12:46

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:

.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. Thanks for all the help!

两者都没有效果。此外,我尝试将z-index设为2但仍无法关闭警报。感谢您的帮助!

PS: I am using this in a chromeapp that I am developing

PS:我正在开发的chromeapp中使用它

1 个解决方案

#1


1  

Since you are using AngularJs with Bootstrap, why not use Angular UI for Bootstrap?

由于您使用AngularJs和Bootstrap,为什么不使用Angular UI进行Bootstrap?

You can add a ui-bootstrap-min.js file to your scripts and add a controller for the alert as below. The HTML will be:

您可以将ui-bootstrap-min.js文件添加到脚本中,并为警报添加控制器,如下所示。 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函数,但你应该能够在它上面构建以获得解决方案。

#1


1  

Since you are using AngularJs with Bootstrap, why not use Angular UI for Bootstrap?

由于您使用AngularJs和Bootstrap,为什么不使用Angular UI进行Bootstrap?

You can add a ui-bootstrap-min.js file to your scripts and add a controller for the alert as below. The HTML will be:

您可以将ui-bootstrap-min.js文件添加到脚本中,并为警报添加控制器,如下所示。 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函数,但你应该能够在它上面构建以获得解决方案。