在angularjs如何访问触发事件的元素? [英] in angularjs how to access the element that triggered the event?

查看:353
本文介绍了在angularjs如何访问触发事件的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我同时使用引导和AngularJS在我的web应用程序。我有一些困难得到两个一起工作。

我有一个元素,它具有属性数据提供=预输入

 <输入ID =SEARCHTEXTNG模型=SEARCHTEXT类型=文本
       类=输入搜索中查询占位=标题
       数据提供=预输入NG变化=updateTypeahead()/>

和我想更新数据源属性时,该领域的用户输入。功能 updateTypeahead 正确触发,但我没有访问触发事件,除非我用 $('#SEARCHTEXT元素),这是jQuery的方式,而不是AngularJS方式

什么是让AngularJS与旧式JS模块工作的最佳方式。


解决方案

  updateTypeahead(本)

不会DOM元素传递给函数 updateTypeahead(本)。在这里,这个将参考范围。如果您要访问的DOM元素使用 updateTypeahead($事件)。在回调函数可以通过 event.target 获得DOM元素。

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.

解决方案

 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.

这篇关于在angularjs如何访问触发事件的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆