Search code examples
javascriptangularjsangularjs-directivetypeahead.jsng-tags-input

AngularJS - Using typeahead with ngTagsInput


This is my 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 was working fine until I removed the input tag (on which it was an attribute) and added the tags-input directive. So, I had <input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;"> earlier. Now, I have <tags-input placeholder="To" ng-model="select" typeahead="toPerson for toPerson in to | filter:$viewValue" style="width:95%;"> and it does not work.

I think this happened because typeahead only works on input tags. Does anyone have ideas on how to make typeahead work with ngTagsInput?

Just to avoid any confusion, I want to make it clear that, I know how to use autocomplete with ngTagsInput. My question is specifically targeting problems I am facing using typeahead with ngTagsInput.

Here is a plunker: http://plnkr.co/edit/B1aV9TLu58a2iGhpTUrN?p=preview

UPDATE

I solved the problem using angular-tags instead of ngTagsInput. ngTagsInput does not work with typeahead. The best answer that was chosen provides a good explanation of why this is the case. Here is the Plunker with the solution to this issue using angular-tags - http://plnkr.co/edit/PaG1k5N37BTOflObnN7K?p=preview


Solution

  • ngTagsInput will not work with ui.bootstrap.typeahead because it generates the input elements itself and the typeahead attribute doesn't end up on the generated input element.

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

    ends up being:

    <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 does not seem to provide a way for this to be achieved either. Since it provides it's own search mechanism it probably doesn't allow using an external one.