davidshen84 davidshen84 - 7 months ago 13
Javascript Question

in angularjs how to access the element that triggered the event?

I use both Bootstrap and AngularJS in my web app. I'm having some difficulty getting the two to work together.

I have an element, which has the attribute

data-provide="typeahead"


<input id="searchText" ng-model="searchText" type="text"
class="input-medium search-query" placeholder="title"
data-provide="typeahead" ng-change="updateTypeahead()" />


And I want to update the
data-source
attribute when the user inputs in the field. The function
updateTypeahead
is triggered correctly, but I don't have access to the element that triggered the event, unless I use
$('#searchText')
, which is the jQuery way, not the AngularJS way.

What is the best way to get AngularJS to work with old style JS module.

Answer
 updateTypeahead(this)

will not pass DOM element to the function updateTypeahead(this). Here this will refer to the scope. If you want to access the DOM element use updateTypeahead($event). In the callback function you can get the DOM element by event.target.

Blockquote Please Note : ng-change function doesn't allow to pass $event as variable.

Comments