AngularJS全局警告框实现方法示例

 更新时间:2017年05月18日 08:52:39   作者:timelessmemoryli  
这篇文章主要介绍了AngularJS全局警告框实现方法,结合实例形式分析了AngularJS全局警告框的实现步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了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" rel="external nofollow" >
  <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>

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结

希望本文所述对大家AngularJS程序设计有所帮助。

相关文章

  • 详解Angular6学习笔记之主从组件

    详解Angular6学习笔记之主从组件

    这篇文章主要介绍了详解Angular6学习笔记之主从组件,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • AngularJS基础 ng-class-odd 指令示例

    AngularJS基础 ng-class-odd 指令示例

    本文主要介绍AngularJS ng-class-odd 指令,这里对ng-class-odd基础知识做了详细整理,并有示例代码和效果图,学习AngularJS的同学可以参考下
    2016-08-08
  • AngularJS入门教程之路由机制ngRoute实例分析

    AngularJS入门教程之路由机制ngRoute实例分析

    这篇文章主要介绍了AngularJS入门教程之路由机制ngRoute,结合实例形式分析了AngularJS路由机制的原理、ngRoute的组成、配置、参数与相关使用技巧,需要的朋友可以参考下
    2016-12-12
  • angularjs使用directive实现分页组件的示例

    angularjs使用directive实现分页组件的示例

    本篇文章主要介绍了angularjs使用directive实现分页组件的示例,具有一定的参考价值,有兴趣的可以了解一下。
    2017-02-02
  • AngularJS基础 ng-include 指令示例讲解

    AngularJS基础 ng-include 指令示例讲解

    本文主要介绍AngularJS ng-include 指令,这里对ng-include 指令的知识做了详细整理介绍,有需要的朋友可以参考下
    2016-08-08
  • AngularJS学习笔记之ng-options指令

    AngularJS学习笔记之ng-options指令

    ng-options是angular-1.3新出的一个指令,这篇文章就来介绍这个指令的用法.有需要的小伙伴可以参考下。
    2015-06-06
  • ng-alain表单使用方式详解

    ng-alain表单使用方式详解

    这篇文章主要介绍了ng-alain表单使用方式详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • AngularJS折叠菜单实现方法示例

    AngularJS折叠菜单实现方法示例

    这篇文章主要介绍了AngularJS折叠菜单实现方法,结合完整实例形式分析了AngularJS实现折叠菜单的具体步骤与相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • AngularJS HTML编译器介绍

    AngularJS HTML编译器介绍

    这篇文章主要介绍了AngularJS HTML编译器介绍,AngularJS的HTML编译器能让浏览器识别新的HTML语法。它能让你将行为关联到HTML元素或者属性上,甚至能让你创造具有自定义行为的新元素,需要的朋友可以参考下
    2014-12-12
  • AngularJs 国际化(I18n/L10n)详解

    AngularJs 国际化(I18n/L10n)详解

    本文主要介绍AngularJs 国际化的知识,这里整理了详细的资料来讲解国际化,有需要的小伙伴可以参考下
    2016-09-09

最新评论