带有确认弹出窗口的自定义指令按钮

时间:2016-01-13 05:56:49

标签: angularjs twitter-bootstrap angularjs-directive

我有一个按钮指令如下(Plunker是here):

<button type="button" 
        data-confirm-popup-btntext="Reject"
        data-confirm-popup-text="Do you want to reject" 
        data-confirm-popup-header="Reject"
        data-confirm-popup-click="reject(obj)" 
        class="btn btn-danger btn-xs" 
        data-ng-class="{disabled : disable}"
        data-ng-if="show"></button>

我有按钮文字data-confirm-popup-btntext。我不想要的。我也不想要data-confirm-popup-click。相反,我想使用ng-click

我的概念是,任何视图都会有任何按钮。如果我需要在处理之前显示一个确认对话框,我将向该按钮添加一个属性(指令),该指令将关注所有内容。

此外,我无法在当前实施中添加<span class'bootstrap-icon'></span> Reject

所以我期望的指令结果如下:

<button type="button" 
      data-confirm-popup-header="Reject"
      data-confirm-popup-text="Do you want to reject" 
      <!--Above line two line will add confirm dialog functionality -->
      data-ng-click="reject(obj)" 
      class="btn btn-danger btn-xs" 
      data-ng-class="{disabled : disable}"
      data-ng-if="show"><span>Any HTML</span>Reject</button>

我尝试使用替换false和true进行trnsculation但无法实现此功能。

3 个答案:

答案 0 :(得分:1)

我使用了你的plunker并改变了app.js,从第14行移到了最后并替换了这个指令应该让你达到你想要的地步;

app.directive("confirmPopupText",confirmPopupText);
    confirmPopupText.$inject = ['$uibModal', '$compile', '$parse'];
    function confirmPopupText (  $modal,   $compile, $parse){
        var directive = {};
        directive.restrict = 'A';
        directive.link= function(scope, elem, attrs) {

            // get reference of ngClick func
            var model = $parse(attrs.ngClick);

            // remove ngClick and handler func        
            elem.prop('ng-click', null).off('click');

            elem.bind('click' , function(e) {
                e.stopImmediatePropagation();
                console.log('Clicked');

                $modal.open({
                    template: '<div class="modal-header">'+attrs.confirmPopupHeader+'</div>'+'<div class="modal-body">'+attrs.confirmPopupText+'</div>'+'<div class="modal-footer">'+'<button class="btn btn-primary" data-ng-click="ok()">Yes</button>'+'<button class="btn btn-warning" data-ng-click="cancel()">No</button>'+'</div>',
                    controller: function($scope, $uibModalInstance) {
                        $scope.ok = function () {
                             $uibModalInstance.close();

                             // this line will invoke ngClick func from outer scope
                             model(scope);
                        };
                        $scope.cancel = function () {
                          $uibModalInstance.dismiss('cancel');
                        };
                    }
                });

            });
        };
        return directive; 
    }

这样你就可以使用那样的html;

<button type="button" 
  data-confirm-popup-header="Reject"
  data-confirm-popup-text="Do you want to reject" 
  <!--confirmPopupText directive will add confirm dialog functionality -->
  data-ng-click="reject(obj)" 
  class="btn btn-danger btn-xs" 
  data-ng-class="{disabled : disable}"
  data-ng-if="show"><span>Any HTML</span>Reject</button>

更新了plunker链接:https://plnkr.co/edit/Bmcqqe32Px25pFCf0Mus?p=preview

答案 1 :(得分:0)

link函数中,您可以取消/注册事件处理程序并执行任何DOM操作。因此,您可以在此处添加确认功能以及按钮内容和类。要重新绑定ng-click处理程序,请在绑定自己的处理程序之前取消绑定“click”事件。

return {    
    priority: 1,
    compile: function(elem, attr) {
      var text = attr.confirmPopupText;
      var callback = $parse(attr.ngClick);
      elem.html('<span>Any HTML</span>Reject');
      elem.addClass('btn btn-danger btn-xs');  

      return function(scope, elem, attr) {
        elem.unbind('click').bind('click', function(event) {
          var result = $window.confirm(text);  // use $modal here instead        
          callback(scope, {result: result, $event : event});
          scope.$apply(scope.ngClick);
        });
      }
    }
}

您可以将此指令用作

<div ng-app="app" ng-controller="Main as view">
  <button data-confirm-popup-text="Do you want to reject"  
          confirm-button ng-click="view.onConfirm($event, result)">
  </button>
</div>

https://jsfiddle.net/9huoL1wL/4/

答案 2 :(得分:0)

基于Erik Chen's answer,您可以覆盖或修改AngularJS中的任何服务,包括指令:

app.config(function($provide){
   $provide.decorator('ngClickDirective', ['$delegate', function($delegate) {
      //$delegate is array of all ng-click directive
      //in this case frist one is angular buildin ng-click
      //so we remove it.
      $delegate.shift();
      return $delegate;
   }]);
});

app.directive('ngClick', function($rootScope) {
  return {
    restrict: 'A',
    priority: 100,
    link: function($scope, element, attr) {
      element.bind('click', function($event) {
        // emit event to manage modal if 'popup' attr is exist
        if (attr.hasOwnProperty('popup')) {
            // and pass arguments
            $scope.$emit('popup-click', { $scope, element, attr }); 
        } else {
            // else just execute default 'ng-click' handler
          $scope.$apply(attr.ngClick)
        }
      })
    }
  }
})

管理工厂的弹出窗口:

app.factory('popupService', function($rootScope) {
    $rootScope.$on('popup-click', function(e, args) {
        // click with popup attr observer
        // there needs to be your code to manage modal
        // you can pass any params for example as 'popup-title="i am a title"' and get it there with 'args.attr'
        if (confirm(args.attr.popupText || 'Default text')) {
            args.$scope.$apply(args.attr.ngClick)
        } else {
            // nothing to do
        }
    });
    return {};
});

我已创建JSFiddle以显示完整示例。

希望,它会对你有帮助。

相关问题