简体   繁体   English

AngularJS Alert在DOM中,应该显示但不显示

[英]AngularJS Alert is in DOM, should show but doesn't

I have a simple test alert in my page. 我的页面上有一个简单的测试警报。 I can see the markup generated in the page DOM. 我可以看到在页面DOM中生成的标记。 It looks to me like it should show but it doesn't. 在我看来,它应该显示但不是。 Here is my code... 这是我的代码...

Controller 控制者

在此处输入图片说明

Html HTML

在此处输入图片说明

Chrome console Chrome控制台

在此处输入图片说明

Looking at the console output, should the alert be visible? 查看控制台输出,警报应该可见吗? It isn't. 不是。 I'd sure appreciate an expert's advice. 我一定会感谢专家的建议。

Thanks for looking everyone :-) 谢谢大家的光临:-)

EDIT... 编辑...

Here is my main app.js file 这是我的主要app.js文件

在此处输入图片说明

Here is a better Controller clip 这是一个更好的控制器剪辑

在此处输入图片说明

EDIT 2... 编辑2 ...

Here is computed style 这是计算风格

在此处输入图片说明

Assuming you use UI Bootstrap, you need to create a closeAlert function (which probably throw an undefined error) in your controller like this : 假设您使用UI Bootstrap,则需要在控制器中创建一个closeAlert函数(可能会引发未定义的错误),如下所示:

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

Also don't forget to add the ui.bootstrap dependency in your app 同样不要忘记在应用程序中添加ui.bootstrap依赖项

Edit : 编辑:

Make sure your element is not in display none :) 确保您的元素未显示为无:)

Here is my working code. 这是我的工作代码。 I wanted to share. 我想分享。 Big thanks to Apercu for getting me over the hump :-) The html goes in your view.html somewhere, maybe at top, and add the AlertService to your app and inject in the controller like normal... 非常感谢Apercu使我摆脱困境 :-) html进入了您的view.html中的某个位置,也许位于顶部,并将AlertService添加到您的应用中,并像往常一样注入到控制器中...

In your controller 在您的控制器中

$scope.closeAlert = function (index) {
    AlertService.closeAlert(index);
};

Alert html 警报html

<alert ng-repeat="alert in alerts" type="alert.type" close="closeAlert($index)">
    <span ng-bind-html="alert.msg"></span>
</alert>

AlertService AlertService

angular.module('AJM.AlertService', ['ui.bootstrap'])
.service('AlertService', ['$rootScope', '$sce', '$timeout', function ($rootScope, $sce, $timeout) {

    $rootScope.alerts = []; 

    var self = this;

    var addAlert = function (type, title, msg, seconds) {
        var index = $rootScope.alerts.length;
        $rootScope.alerts.push({ "type":type, "msg":$sce.trustAsHtml('<strong>' + title + '</strong> ' + msg) });
        if (seconds) {
            $timeout(function (index) {
                self.closeAlert(index);
            }, (seconds * 1000));
        }
        return true;
    };

    this.success = function (title, msg, seconds) {
        addAlert('success', title, msg, seconds);
    };

    this.info = function (title, msg, seconds) {
        addAlert('info', title, msg, seconds);
    };

    this.warning = function (title, msg, seconds) {
        addAlert('warning', title, msg, seconds);
    };

    this.danger = function (title, msg, seconds) {
        addAlert('danger', title, msg, seconds);
    };

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

}]);

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

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