admin管理员组文章数量:1296432
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?
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?
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?
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?
- create a directive which binds the event to element – Naeem Shaikh Commented Dec 18, 2014 at 14:10
- @NaeemShaikh I've seen lots of posts and directive tutorials but I'm still not sure how to implement it. What does directives do here? How to pass event arguments? – Melkor Commented Dec 18, 2014 at 14:29
2 Answers
Reset to default 7see 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
You could do the following:
- Wrap your custom element inside an
auto-binding
template. - Bind all handlers from angular scope to polymer scope (template element).
And that's it!
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://www.polymer-project/ponents/polymer/polymer.html" rel="import">
<link href="https://www.polymer-project/ponents/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./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://www.polymer-project/ponents/polymer/polymer.html" rel="import">
<link href="https://www.polymer-project/ponents/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.
本文标签: javascriptHow to bind custom events in AngularJSStack Overflow
版权声明:本文标题:javascript - How to bind custom events in AngularJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741637548a2389728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论