如何在AngularJS中绑定自定义事件?

时间:2022-05-10 03:14:17

I have an custom event core-transitionend (actually fired by Polymer), and I can set an event handler using document.addEventListener(). But what's the best practice to do it in AngularJS?

我有一个自定义事件core-transitionend(实际上是由Polymer解雇),我可以使用document.addEventListener()设置一个事件处理程序。但是在AngularJS中最好的做法是什么?

Or, I can explicitly set a handler in DOM, i.e. <paper-ripple on-core-transitionend="enter()"></paper-ripple>, but how to define this function in AngularJS?

或者,我可以在DOM中明确设置处理程序,即 ,但是如何在AngularJS中定义此函数?

2 个解决方案

#1


7  

see this fiddle, here I have created a custom directive which binds the event to the element,

看到这个小提琴,在这里我创建了一个自定义指令,将事件绑定到元素,

angular.module('HelloApp', [])
    .directive('customDir', function () {
        return {
            restrict: 'A',

            link: function(scope, element, attrs)      
            {
                element.bind("click",function()
            {
            alert("you clicked me");

        })
            }    


        }
    })

In your case you can simply bind your defined event to the element

在您的情况下,您可以简单地将定义的事件绑定到元素

#2


1  

You could do the following:

您可以执行以下操作:

  1. Wrap your custom element inside an auto-binding template.
  2. 将自定义元素包装在自动绑定模板中。

  3. Bind all handlers from angular scope to polymer scope (template element).
  4. 将所有处理程序从角度范围绑定到聚合物范围(模板元素)。

And that's it!

就是这样!

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import">

<link href="https://www.polymer-project.org/components/paper-button/paper-button.html" rel="import">
<div ng-app="demo-app">
  <div ng-controller="DemoController">
    <template bind-events="clickMe,mouseOver" is="auto-binding">
      <paper-button raised on-tap="{{clickMe}}" on-mouseover="{{mouseOver}}">click me</paper-button>
    </template>
    <pre>
            <code>
            {[{text}]}
            </code>
            </pre>
  </div>
</div>
<script>
  angular.module('demo-app', [])
    .config(function($interpolateProvider) {
      $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
    })
    .directive('bindEvents', function() {
      return {
        restrict: 'A',
        link: function(scope, element, attrs) {
          eventNames = attrs.bindEvents.split(',');
          eventNames.forEach(function(eventName) {
            element[0][eventName] = scope[eventName];
          });
        }
      }
    })
    .controller('DemoController', function($scope) {
      $scope.text = '';
      $scope.clickMe = function() {
        $scope.text += '\nyou clicked me!!';
        $scope.$apply();
      };
      $scope.mouseOver = function() {
        $scope.text += '\nyou hovered me!!';
        $scope.$apply();
      }
    });
</script>

Or if it's not an issue to copy the whole scope you can:

或者,如果复制整个范围不是问题,您可以:

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import">

    <link href="https://www.polymer-project.org/components/paper-button/paper-button.html" rel="import">
    <div ng-app="demo-app">
      <div ng-controller="DemoController">
        <template bind-angular-scope is="auto-binding">
          <paper-button raised on-tap="{{clickMe}}" on-mouseover="{{mouseOver}}">click me</paper-button>
        </template>
        <pre>
                <code>
                {[{text}]}
                </code>
                </pre>
      </div>
    </div>
    <script>
      angular.module('demo-app', [])
        .config(function($interpolateProvider) {
          $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
        })
        .directive('bindAngularScope', function() {
        	return {
                restrict: 'A',
                link: function(scope, element, attrs) {
                    for(k in scope) {
                    	if (!element[0][k]) {
                    		element[0][k] = scope[k];
                    	}
                    }
                }
            }
        })
        .controller('DemoController', function($scope) {
          $scope.text = '';
          $scope.clickMe = function() {
            $scope.text += '\nyou clicked me!!';
            $scope.$apply();
          };
          $scope.mouseOver = function() {
            $scope.text += '\nyou hovered me!!';
            $scope.$apply();
          }
        });
    </script>

Notice: that I had to change Angular's interpolation symbol to get them to work together.

注意:我必须更改Angular的插值符号才能使它们一起工作。

#1


7  

see this fiddle, here I have created a custom directive which binds the event to the element,

看到这个小提琴,在这里我创建了一个自定义指令,将事件绑定到元素,

angular.module('HelloApp', [])
    .directive('customDir', function () {
        return {
            restrict: 'A',

            link: function(scope, element, attrs)      
            {
                element.bind("click",function()
            {
            alert("you clicked me");

        })
            }    


        }
    })

In your case you can simply bind your defined event to the element

在您的情况下,您可以简单地将定义的事件绑定到元素

#2


1  

You could do the following:

您可以执行以下操作:

  1. Wrap your custom element inside an auto-binding template.
  2. 将自定义元素包装在自动绑定模板中。

  3. Bind all handlers from angular scope to polymer scope (template element).
  4. 将所有处理程序从角度范围绑定到聚合物范围(模板元素)。

And that's it!

就是这样!

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import">

<link href="https://www.polymer-project.org/components/paper-button/paper-button.html" rel="import">
<div ng-app="demo-app">
  <div ng-controller="DemoController">
    <template bind-events="clickMe,mouseOver" is="auto-binding">
      <paper-button raised on-tap="{{clickMe}}" on-mouseover="{{mouseOver}}">click me</paper-button>
    </template>
    <pre>
            <code>
            {[{text}]}
            </code>
            </pre>
  </div>
</div>
<script>
  angular.module('demo-app', [])
    .config(function($interpolateProvider) {
      $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
    })
    .directive('bindEvents', function() {
      return {
        restrict: 'A',
        link: function(scope, element, attrs) {
          eventNames = attrs.bindEvents.split(',');
          eventNames.forEach(function(eventName) {
            element[0][eventName] = scope[eventName];
          });
        }
      }
    })
    .controller('DemoController', function($scope) {
      $scope.text = '';
      $scope.clickMe = function() {
        $scope.text += '\nyou clicked me!!';
        $scope.$apply();
      };
      $scope.mouseOver = function() {
        $scope.text += '\nyou hovered me!!';
        $scope.$apply();
      }
    });
</script>

Or if it's not an issue to copy the whole scope you can:

或者,如果复制整个范围不是问题,您可以:

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import">

    <link href="https://www.polymer-project.org/components/paper-button/paper-button.html" rel="import">
    <div ng-app="demo-app">
      <div ng-controller="DemoController">
        <template bind-angular-scope is="auto-binding">
          <paper-button raised on-tap="{{clickMe}}" on-mouseover="{{mouseOver}}">click me</paper-button>
        </template>
        <pre>
                <code>
                {[{text}]}
                </code>
                </pre>
      </div>
    </div>
    <script>
      angular.module('demo-app', [])
        .config(function($interpolateProvider) {
          $interpolateProvider.startSymbol('{[{').endSymbol('}]}');
        })
        .directive('bindAngularScope', function() {
        	return {
                restrict: 'A',
                link: function(scope, element, attrs) {
                    for(k in scope) {
                    	if (!element[0][k]) {
                    		element[0][k] = scope[k];
                    	}
                    }
                }
            }
        })
        .controller('DemoController', function($scope) {
          $scope.text = '';
          $scope.clickMe = function() {
            $scope.text += '\nyou clicked me!!';
            $scope.$apply();
          };
          $scope.mouseOver = function() {
            $scope.text += '\nyou hovered me!!';
            $scope.$apply();
          }
        });
    </script>

Notice: that I had to change Angular's interpolation symbol to get them to work together.

注意:我必须更改Angular的插值符号才能使它们一起工作。