[英]How do I catch Polymer events in an Angular app?
I use angularjs and polymer together, because polymer doesn't have enough material design ui elements, and angularjs has a material design project ( https://material.angularjs.org ) in order to implement the material design system.我将angularjs和polymer一起使用,因为polymer没有足够的材料设计ui元素,而angularjs有一个材料设计项目( https://material.angularjs.org )来实现材料设计系统。
HTML ( ng-app="list"
ng-controller="showTasks"
): HTML ( ng-app="list"
ng-controller="showTasks"
):
<md-content class="md-padding">
<md-list>
<md-item ng-repeat="task in tasks">
<md-item-content>
<div class="md-tile-left">
<paper-icon-button icon="assignment"></paper-icon-button>
</div>
<div class="md-tile-content">
<h3>{{task.title}}</h3>
<h4><strong>Due: </strong>{{task.due}}</h4>
<p>
<strong>Subjects: </strong>{{task.subjects}}
</p>
</div>
<paper-ripple class="recenteringTouch" core-transitionend="" fit></paper-ripple>
</md-item-content>
</md-item>
</md-list>
</md-content>
Script:脚本:
<script>
var app = angular.module('list', ['ngMaterial']);
app.controller('showTasks', ['$scope', '$http', function($scope, $http) {
$scope.tasks = [];
$http.post('/getAllTasks').success(function(response) {
if (response.ok == 1) {
$scope.tasks = response.result;
}
});
}]);
</script>
paper-ripple
element has an event core-transitionend
. paper-ripple
元素有一个事件core-transitionend
。 It can be used in polymer-element by setting the attribute core-transitioned={{event-handler}}
and register the handler Polymer('element-name', {event-handler: function() {}})
.它可以通过设置属性core-transitioned={{event-handler}}
并注册处理程序Polymer('element-name', {event-handler: function() {}})
在Polymer('element-name', {event-handler: function() {}})
。
But here angularjs will parse {{}}
statement, and I can't register paper-ripple
because it has been registered.但是这里angularjs会解析{{}}
语句,我无法注册paper-ripple
因为它已经注册了。
So how can I catch this event?那么我怎样才能捕捉到这个事件呢?
If you want to stop angular from parsing the {{}}
statement, try using {{'{{}}'}}
.如果您想阻止 angular 解析{{}}
语句,请尝试使用{{'{{}}'}}
。 That might be all you need to get what you need from polymer.这可能就是您从聚合物中获得所需的全部内容。
Here's something to try:这里有一些东西可以尝试:
<paper-ripple class="recenteringTouch"
core-transitionend="{{'{{event-handler}}'}}"
fit>
</paper-ripple>
Well, I figured it out.嗯,我想通了。
use directive
, and in the link
function:使用directive
,并在link
函数中:
element.on('click', function() {
element.on('core-transitionend', function() {
// method 1 processing
});
});
<paper-ripple class="recenteringTouch" ng-click="rippleClicked($event)" fit></paper-ripple>
$scope.rippleClicked = function($event) {
angular.element($event.target).on('core-transitionend', function() {
// method 2 processing
});
}
But the two methods are not the same.但是这两种方法并不相同。 For method 1, when you click on the element, hold the mouse, and release it outside the element, the method 1 processing
will not trigger (I don't know why, it seems like the click
event is not triggered), while method 2 will.对于方法一,当你点击元素,按住鼠标,释放到元素外时, method 1 processing
不会触发(不知道为什么,好像没有触发click
事件),而方法一2 会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.