Custom class in array loop not working

I am trying to use a class ( question ) that will be attached in each element of the array, the loop works fine. My problem is that when I try to use a class in jQuery nothing happens, it is as if it is not ...

I know that Angular adds ng-scope and ng-binding classes, can this prevent jQuery, maybe?

When I check the DOM, there are no errors and my class is not there, it just doesn't work!

Here is my code: HTML

 <p ng-repeat="n in ['Human or Lemar King?','Name of your tribe?','Can you Boogie?'] | filter:query" class="question"> {{n}}</p> 

JQuery

 $(function () { $(".question").on('click',function() { alert("Ohh hail King Julien!") }); }); 
+6
source share
1 answer

You are trying to install an event handler on elements that are not already in the DOM tree.

You can either go angular and add ng-click :

 <p ng-click="doSomething()" ng-repeat="n in ['Human or Lemar King?','Name of your tribe?','Can you Boogie?'] | filter:query" class="question"> {{n}}</p> 

Or you can set the live event to <body> (or in the closest parent that is present in the DOM):

 $(function () { $('body').on('click', '.question', function() { alert("Ohh hail King Julien!") }); }); 
+3
source

Source: https://habr.com/ru/post/950785/


All Articles