【发布时间】:2014-06-13 17:11:41
【问题描述】:
我正在使用自定义 Angular.js 指令来包装 Twitter Typeahead。打字功能似乎工作得很好,但是似乎根本没有触发任何自定义事件。我正在使用 Angular 1.2.6、Twitter Typeahead 0.10.2 和 jQuery 1.10.2。这是我的代码的几个sn-ps:
search-doctors.html
<div class="controls">
<div class="span11 input-append">
<input type="search" class="span10" name="practiceName" ng-model="message.practiceName" required typedown="contactDoctors" />
<span class="add-on search-glass"><i class="fa fa-search"></i></span>
</div>
</div>
typedown.coffee
angular.module('someApp')
.directive('typedown', ->
restrict: 'A'
link: (scope, element, attrs) ->
element.typeahead({
name: attrs.typedown
source: (query, cb) ->
# Ultimately, this will call to a service that will return the array of
# search results, but for simplicity's sake, I'm stubbing some dummy data.
cb ['Neurocare of Scottsdale', 'Neurocare of Tempe']
})
# These never seem to get fired, despite following the instructions in
# typeahead's documentation
.on 'typeahead:opened', ->
alert 'opened'
.on 'typeahead:autocompleted typeahead:selected', (event, query) ->
alert query
)
typedown 实际上似乎在 UI 上起作用。当我在搜索框中输入“神经”时,我得到了预期的行为,但是当我点击选项卡或单击自动完成选项时,相应的事件处理程序不会触发。此外,当我开始打字时,打字打开时,预期的typeahead:opened 事件没有触发。
在对指令的元素调用typeahead 之前,我尝试将事件绑定到元素。我也尝试过使用bind 而不是on(尽管这应该没有任何效果)。无论如何,我已经没有时间和想法来完成这项工作了。我很想进行一次快速的健全性检查和一些额外的眼睛,看看是否有我遗漏的东西。
提前致谢!
【问题讨论】:
-
AngularUI 的人已经在创建 a wrapper for typeahead for AngularJS 方面做得很好,我真的建议你在自己编写之前先检查一下。
-
感谢您的建议。虽然它看起来很棒,但我决定不使用 Angular UI 项目,只是为了限制我在这个项目上获得的第三方依赖项的数量(这已经相当重要)。当我将 Bootstrap 升级到 3.0 时可能值得重新审视。
标签: javascript angularjs coffeescript twitter-typeahead