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

时间:2014-12-18 12:58:30

标签: javascript angularjs javascript-events polymer

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

或者,我可以在DOM中明确设置处理程序,即<paper-ripple on-core-transitionend="enter()"></paper-ripple>,但如何在AngularJS中定义此函数?

2 个答案:

答案 0 :(得分:7)

请参阅this小提琴,这里我创建了一个将事件绑定到元素的自定义指令,

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

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

        })
            }    


        }
    })

在您的情况下,您只需将定义的事件绑定到元素

即可

答案 1 :(得分:1)

您可以执行以下操作:

  1. 将自定义元素包含在auto-binding模板中。
  2. 将所有处理程序从角度范围绑定到聚合物范围(模板元素)。
  3. 那就是它!

    &#13;
    &#13;
    <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>
    &#13;
    &#13;
    &#13;

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

    &#13;
    &#13;
        <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>
    &#13;
    &#13;
    &#13;

    注意:我必须更改Angular的interpolation symbol才能让他们一起工作。