【问题标题】:Twitter Typeahead custom events aren't firingTwitter Typeahead 自定义事件未触发
【发布时间】: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


【解决方案1】:

在花了几天时间处理其他事情之后,我又回到了我身边。我突然想到,由于我已经在使用 Bootstrap 2.3.2,因此包含 Twitter Typeahead 可能会导致与管理 $.fn.typeahead 的脚本发生冲突。我设法通过删除 Twitter Typeahead 并依靠 Bootstrap 2.3.2 中内置的遗留实现来使其工作。我最终会升级到 Bootstrap 3,但现在这个要求有点高,而且该项目将用于医院,这些医院在网络技术方面是出了名的落后。

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2018-01-12
    相关资源
    最近更新 更多