Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trigger equivalent in AngularJS

I am using AngularJS in my project and I don't want to include jQuery. I want to perform jQuery equivalent of this in AngularJS

$('.someclass').trigger('create');

I searched in internet but couldn't find one.

like image 255
Abhinav Kulkarni Avatar asked Jul 08 '14 10:07

Abhinav Kulkarni


1 Answers

AngularJS has jqLite built into it. Refer the docs to see the available methods

Angular jqLite

For your scenario:

$ will not work in angularJS, angular.element is the equivalent of $. but jqLite being very limited does not support "selectors by id" meaning you cannot simply use angular.element to select an element by class or id like

var element = angular.element('#foo')

you have to use the following

var element = angular.element(document.querySelector('#foo'))

You may ask, "why not use document.getElementById('#foo')?" but remember we are using jqLite so we have to use angular.element in order to be able to use the jqLite methods on the element.

You can use the .triggerHandler() instead of .trigger(). jQuery triggerHandler docs

jqLite does not support click(). The workaround would be to use .ready()

element.ready(function() {
    console.log('Click');
});
like image 82
Dinesh Avatar answered Oct 01 '22 18:10

Dinesh