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,也就是那个蓝色的

效果

我的效果就是这样啦~

posted @ 2016-04-30 13:11  cench  阅读(942)  评论(0编辑  收藏  举报