admin管理员组文章数量:1336293
I am developing an app with onsen UI (angular based) + phonegap. So I have a JQuery click event inside an angular controller and it is the problem.
When I back page stack and go again, It triggers the click event many times.
I am using jquery click event inside angular controller because I must use a Onsen UI Service inside this event. So I must declare this event inside the controller. But this is the problem. Look:
module.controller('ListController', function($scope, EventsService, OneEventService) {
$scope.events = EventsService.getPopular();
//this is the event that is triggering many times
$(document).on("click",".item-from-list",function(e){...}
I tried to use $(element).click(func)
but It cannot get the DOM element.
Then I use $(document).on("click", elem, func)
So how can I fix this?
I am developing an app with onsen UI (angular based) + phonegap. So I have a JQuery click event inside an angular controller and it is the problem.
When I back page stack and go again, It triggers the click event many times.
I am using jquery click event inside angular controller because I must use a Onsen UI Service inside this event. So I must declare this event inside the controller. But this is the problem. Look:
module.controller('ListController', function($scope, EventsService, OneEventService) {
$scope.events = EventsService.getPopular();
//this is the event that is triggering many times
$(document).on("click",".item-from-list",function(e){...}
I tried to use $(element).click(func)
but It cannot get the DOM element.
Then I use $(document).on("click", elem, func)
So how can I fix this?
Share edited Oct 11, 2015 at 7:00 Sherali Turdiyev 1,74316 silver badges29 bronze badges asked Oct 11, 2015 at 6:03 Dyego OviedoDyego Oviedo 3084 silver badges15 bronze badges1 Answer
Reset to default 8This is because you're registering your click
handler on document everytime your controller is initialized. Each time you navigate to some other page/view and e back to the same page again, the controller is initialized (i.e all the code in your controller is executed), so jQuery is adding a new click event listener every time on the document.
You can resolve this in 2 ways.
Option-1:
Using jQuery's off()
to de-register the existing click handlers before adding a new one.
`$(document).off().on("click",".item-from-list",eventHandlerFn);`
Option-2:
Using Angular's scope based event handlers like ng-click
which are de-registered automatically when controller goes out of scope i.e when you move to new page.
<div class='item-from-list' ng-click='handleClick()'></div>
module.controller('ListController', function($scope) {
$scope.handleClick= function(){ /* handle click event here */ };
}
One of the fundamental philosophies of AngularJS is to keep DOM manipulation out of the Controllers, so I suggest the second way to resolve your issue. There's even a third way with custom directives where-in you get a reference to your element and you can register your click handler using jQuery or using the default jQLite's on().
module.directive('registerClick',function(){
return {
restrict : 'A',
link: function (scope, element, attrs) {
/* register your event handler here */
element.on('click', clickHandlerFn);
/* As we're explicitly registering the handler, we need
to de-register it, once your element is destroyed like below */
element.on('$destroy', function () {
element.off('click', clickHandlerFn);
});
}
};
});
本文标签: javascriptJquery click event inside Angular controller issue loop many timesStack Overflow
版权声明:本文标题:javascript - Jquery click event inside Angular controller issue: loop many times - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742406824a2468990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论