angular 指令封装弹出框效果

就直接用bs的警告框啦~,Duang~

功能

  • 可以设置message和type,type就是bs内置的几种颜色

  • 默认提示3秒框自动关闭,或者点击x号关闭

代码

模板

<div class="alert alert-{{type || 'info'}}" ng-show="message"> <button type="button" class="close" ng-click="hideAlert()"> <span class="glyphicon glyphicon-remove"></span> </button> {{message}} </div>

指令

/**
 * 提示框
 */
commonToolDirectives.directive('alertBar',[function(){ return { restrict: 'EA', templateUrl: 'src/common/views/alertBar.html', scope : { message : "=", type : "=" }, link: function(scope, element, attrs){ scope.hideAlert = function() { scope.message = null; scope.type = null; }; } }; }]);

服务

/**
 * 提示框数据
 */
commonServices.factory('TipService', ['$timeout', function($timeout) { return { message : null, type : null, setMessage : function(msg,type){ this.message = msg; this.type = type; //提示框显示最多3秒消失 var _self = this; $timeout(function(){ _self.clear(); },3000); }, clear : function(){ this.message = null; this.type = null; } }; }]);

用法

  1. 因为是全局提示,所以就只有一个,在index.html中添加:

    <!--全局提示框-->
    <alert-bar class="alert_bar" message="tipService.message" type="tipService.type"></alert-bar>

    同时保证他的z-index最高

  2. 然后因为需要在页面上直接访问tipService,需要在最外层controller(如果没有可以直接绑定到$rootScope)中绑定:

    //提示信息服务
    $scope.tipService = TipService;
  3. 调用的时候在c中直接设置message和type就可以了

    TipService.setMessage('登录成功', 'success');

    当然从上面的模板代码可以看到,如果不传第二个参数,type默认是info,也就是那个蓝色的

效果

我的效果就是这样啦~

转载于:https://www.cnblogs.com/cench/p/5448624.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值