【问题标题】:AngularJS - Using typeahead with ngTagsInputAngularJS - 将 typeahead 与 ngTagsInput 一起使用
【发布时间】:2015-08-25 19:45:16
【问题描述】:

这是我的index.html

            <form ng-controller = "emailViewController">
                <tags-input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;">
                </tags-input>
                <br />
                <input type="text" placeholder="Subject" style="width:95%;"><br />
                <textarea style="width:95%;" rows="10"></textarea>
            </form>

emailViewController.js

(function() {
    'use strict';

    var emailViewController = function (fetchDataService,$scope,$filter) {
        var url = 'app/mock/emails.json';

        fetchDataService.getContent(url)
            .then(function(response){
               $scope.emails = response.data;

                $scope.to = [];

                angular.forEach($scope.emails, function(key, value) {
                   $scope.to.push(key.to);
                })

                $scope.loadEmails('Primary');
          });
         }

    angular.module('iisEmail')
        .controller ('emailViewController',
        ['fetchDataService', '$scope','$filter', emailViewController]);
}());

Typeahead 工作正常,直到我删除了input 标记(它是一个属性)并添加了tags-input 指令。所以,我之前有&lt;input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;"&gt;。现在,我有&lt;tags-input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;"&gt;,但它不起作用。

我认为这是因为 typeahead 仅适用于 input tags。有没有人知道如何让typeaheadngTagsInput 一起工作?

为了避免任何混淆,我想明确一点,我知道如何将autocompletengTagsInput 一起使用。我的问题专门针对我在使用typeaheadngTagsInput 时遇到的问题。

这里是一个笨蛋:http://plnkr.co/edit/B1aV9TLu58a2iGhpTUrN?p=preview

更新

我使用angular-tags 而不是ngTagsInput 解决了这个问题。 ngTagsInput 不适用于 typeahead。选择的最佳答案很好地解释了为什么会这样。这是使用angular-tags - http://plnkr.co/edit/PaG1k5N37BTOflObnN7K?p=preview 解决此问题的 Plunker

【问题讨论】:

  • 考虑给出一个更小的例子来说明问题?也许是个笨蛋? :)
  • 感谢您的评论。我已经编辑了我的问题@toskv
  • 我看到 ngTagsInput 指令确实生成了一些 html,很可能 typeahead 属性不会出现在 input 元素上。运行代码时,您应该能够在生成的 html 中进行检查。 :)
  • 顺便说一句,你也有一个错误,emailViewController 函数缺少一个 } 来正确关闭。也许就是这样。
  • @toskv 我想你不明白我的问题。我已经添加了更多细节,所以希望现在很清楚。

标签: javascript angularjs angularjs-directive typeahead.js ng-tags-input


【解决方案1】:

ngTagsInput 不能与 ui.bootstrap.typeahead 一起使用,因为它自己生成输入元素并且 typeahead 属性不会结束在生成的输入元素上。

&lt;tags-input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;"&gt;

最终成为:

<tags-input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;" class="ng-pristine ng-untouched ng-valid ng-isolate-scope ng-valid-max-tags ng-valid-min-tags ng-valid-leftover-text" aria-autocomplete="list"
aria-expanded="false" aria-owns="typeahead-5-8574">
  <div class="host" tabindex="-1" ng-click="eventHandlers.host.click()" ti-transclude-append="">
    <div class="tags" ng-class="{focused: hasFocus}">
      <ul class="tag-list">
        <!-- ngRepeat: tag in tagList.items track by track(tag) -->
      </ul>
      <input class="input ng-pristine ng-untouched ng-valid" autocomplete="off" ng-model="newTag.text" ng-model-options="{getterSetter: true}" ng-keydown="eventHandlers.input.keydown($event)" ng-focus="eventHandlers.input.focus($event)" ng-blur="eventHandlers.input.blur($event)"
      ng-paste="eventHandlers.input.paste($event)" ng-trim="false" ng-class="{'invalid-tag': newTag.invalid}" ng-disabled="disabled" ti-bind-attrs="{type: options.type, placeholder: options.placeholder, tabindex: options.tabindex, spellcheck: options.spellcheck}"
      ti-autosize="" type="text" placeholder="To" spellcheck="true" style="width: 20px;"><span class="input" style="visibility: hidden; width: auto; white-space: pre; display: none;">To</span>
    </div>
  </div>
</tags-input>

ngTagsInput 似乎也没有提供实现这一目标的方法。由于它提供了自己的搜索机制,因此可能不允许使用外部搜索机制。

【讨论】:

  • 感谢您的回答。我正在尝试重新创建您昨天创建的 plunker (plnkr.co/edit/DmuuzM?p=preview)。但是,我在这样做时遇到了问题。我没有收到任何错误,但它们也没有输出。你有什么想法 ?这是 plunker - plnkr.co/edit/PaG1k5N37BTOflObnN7K
  • 是的,您的 html 中某处缺少 ng-app ;) @Michelle
  • 谢谢。我不知道我是怎么错过的。你知道为什么它不允许我输入任何不在$scope.to 变量中的值吗?
  • 我想通了。只需将addable 选项添加到指令中。这是 plunker - plnkr.co/edit/PaG1k5N37BTOflObnN7K?p=preview
  • 是的..如果您对答案感到满意,请将其标记为解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 2014-10-06
  • 2013-01-25
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多