AngularJS全局警告框实现方法示例
程序员文章站
2022-07-05 20:49:21
本文实例讲述了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">×</span> </button> {{content}} </div>
更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结》
希望本文所述对大家angularjs程序设计有所帮助。
推荐阅读
-
Android编程双重单选对话框布局实现与事件监听方法示例
-
C#实现在前端网页弹出警告对话框(alert)的方法
-
jQuery模拟12306城市选择框功能简单实现方法示例
-
angularjs实现table表格td单元格单击变输入框/可编辑状态示例
-
AngularJS基于provider实现全局变量的读取和赋值方法
-
Angularjs实现带查找筛选功能的select下拉框示例代码
-
Android 开发使用PopupWindow实现弹出警告框的复用类示例
-
jQuery实现点击下拉框中的值累加到文本框中的方法示例
-
Vue组件全局注册实现警告框的实例详解
-
Android编程实现AlertDialog自定义弹出对话框的方法示例