Angularjs全局警告框

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="bootstrap.min.css">
    <script src="jquery.min.js"></script>
    <script src="angular.js"></script>
    <script src="angular-animate.js"></script>
    <script src="bootstrap.min.js"></script>
    <script type="text/javascript">
        var myapp = angular.module('myapp', ['ngAnimate']);
        myapp.controller('msgController', ['$scope', 'notificationService', function($scope, notificationService) {
            $scope.msg = notificationService;
            $scope.show = function() {
                notificationService.danger('success');
            }
        }]);
        myapp.controller('controller', ['$scope', 'notificationService', function($scope, notificationService) {
            $scope.show = function() {
                notificationService.info('info');
            }
        }]);
        myapp.directive('msgBox', function() {
            return {
                restrict : 'EA',
                scope : {
                    content: '@',
                    type: '@',
                },
                templateUrl : 'tmpl.html',
                link : function(scope, elem, attrs) {
                    scope.close = function() {
                        scope.content = '';
                    };
                }
            };
        });
        myapp.factory('notificationService', function($timeout, $rootScope) {
            return {
                content : '',
                type : '',
                success : function(content) {
                    this.tmpl(content, 'success')
                },
                info : function(content) {
                    this.tmpl(content, 'info')
                },
                warning : function(content) {
                    this.tmpl(content, 'warning')
                },
                danger : function(content) {
                    this.tmpl(content, 'danger')
                },
                tmpl : function(content, type) {
                    this.content = content;
                    this.type = type;
                    var _self = this;
                    $timeout(function() {
                        _self.clear();
                    }, 5000);
                },
                clear : function() {
                    this.content = '';
                    this.type = '';
                }
            };
        });
    </script>
    <style type="text/css">
        .msg-box {
            z-index: 666;
            position: absolute;
            width: 100%;
            top: 5px;
        }
        .msg.ng-enter {
            transition: 2s linear all;
            opacity: 0.3;
        }
        .msg.ng-enter-active {
            opacity: 1;
        }
        .msg.ng-leave {
            transition: 2s linear all;
            opacity: 1;
        }
        .msg.ng-leave-active {
            opacity: 0;
        }
    </style>
  </head>
  <body ng-app="myapp" ng-controller="msgController">
     <msg-box content="{{msg.content}}" type="{{msg.type}}" class="msg-box">
     </msg-box>
     <h1>content</h1>
     <button type="button" class="btn btn-primary" ng-click="show()">success</button>
     <div ng-controller="controller">
        <button type="button" class="btn btn-primary" ng-click="show()">info</button>
     </div>
  </body>

</html>


<div class="alert alert-{{type || 'info'}} msg" role="alert" ng-if="content">
  <button type="button" class="close" aria-label="Close" ng-click="close()">
    <span aria-hidden="true">&times;</span>
  </button>
  {{content}}
</div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值