在 angularjs 中如何访问触发事件的元素? [英] in angularjs how to access the element that triggered the event?
问题描述
我在我的网络应用程序中同时使用 Bootstrap 和 AngularJS.我很难让两者一起工作.
I use both Bootstrap and AngularJS in my web app. I'm having some difficulty getting the two to work together.
我有一个元素,它有属性 data-provide="typeahead"
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()" />
我想在用户输入字段时更新 data-source
属性.函数 updateTypeahead
被正确触发,但我无权访问触发事件的元素,除非我使用 $('#searchText')
,这是jQuery 方式,而不是 AngularJS 方式.
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.
让 AngularJS 与旧式 JS 模块一起工作的最佳方法是什么.
What is the best way to get AngularJS to work with old style JS module.
推荐答案
updateTypeahead(this)
不会将 DOM 元素传递给函数 updateTypeahead(this)
.这里 this
将引用范围.如果您想访问 DOM 元素,请使用 updateTypeahead($event)
.在回调函数中可以通过event.target
.
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
.
请注意:ng-change 函数不允许将 $event 作为变量传递.
Please Note : ng-change function doesn't allow to pass $event as variable.
这篇关于在 angularjs 中如何访问触发事件的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!