AngularJS如何从ng-really点击传递事件

时间:2014-06-06 07:21:27

标签: html angularjs coffeescript

我想将事件传递给我的方法,但它会像'undefined'一样到达。这是我的态度

  <button class="btn btn-danger" ng-really-message="Are you sure want to delete?"
         ng-really-click="uc.deleteUser(user.id)">
         <i class="glyphicon glyphicon-trash"></i> </button>

.coffee文件中的方法

    deleteProduct: (id, event) ->
       console.log(event)
       @ProductService.deleteProduct(id)
       .then(
           (data) =>
               @getAllProducts()
           ,
           (error) =>
               @$log.error "Unable to delete Products: #{error}"
           )

但是如果我通过ng-click传递事件就可以了。根据' ng-really click '$ event dias想象出现!请帮帮我。

1 个答案:

答案 0 :(得分:3)

这是支持传递$event的版本:

app.directive('ngReallyClick', ['$parse',
  function($parse) {
    return {
      compile: function(tElement, tAttrs) {
        var fn = $parse(tAttrs.ngReallyClick);
        return function(scope, element, attrs) {
          element.on('click', function(event) {
            var message = attrs.ngReallyMessage;
            if (message && confirm(message)) {
              scope.$apply(function() {
                fn(scope, {
                  $event: event
                });
              });
            }
          });
        };
      }
    };
  }
]);

用法:

ng-really-click="uc.deleteUser(user.id, $event)"