Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tokenInput as angular.js directive

I'm attempting to create an angular.js directive from James Smith's tokenInput Jquery plugin: http://loopj.com/jquery-tokeninput

Here is what I have so far:

antdna = angular.module('Communication', []);

antdna.factory('autoCompleteService', [function() {
    return {
      getSource: function() {
      return [{"id":1, "name":"John Doe"}, {"id":2, "name":"Jane Smith"}];
    }
  }
}]);

antdna.directive('autoComplete', function(autoCompleteService) {
    return {
        restrict: 'A',
        link: function(scope, elem) {
            elem.tokenInput(autoCompleteService.getSource(), {
                crossDomain:false,
                theme: "facebook",
                hintText: "Enter User Name",

                preventDuplicates: true
            });
            }
    };
});

With the following markup:

<input type="text" name="recipient" ng-model="conversation.recipients" class="messageComposeTextField" auto-complete placeholder="To :" required />

TokenInput works perfectly but my issue is that I cannot bind to the model.

{{conversation.recipients}} 

is blank.

The tokenInput plugin generates it's own markup using list elements (ul and li). So upon inspecting the element I get:

<ul class="token-input-list-facebook">
  <li class="token-input-token-facebook"><p>John Doe</p><span class="token-input-delete-token-facebook">×</span></li><li class="token-input-input-token-facebook"><input type="text" autocomplete="off" autocapitalize="off" id="token-input-" style="outline: none; width: 30px;"><tester style="position: absolute; top: -9999px; left: -9999px; width: auto; font-size: 12px; font-family: Ubuntu, 'Ubuntu Beta', UbuntuBeta, Ubuntu, 'Bitstream Vera Sans', 'DejaVu Sans', Tahoma, sans-serif; font-weight: 400; letter-spacing: 0px; white-space: nowrap;"></tester>    </li>
</ul>

<input type="text" name="recipient" ng-model="conversation.recipients" class="messageComposeTextField ng-pristine ng-invalid ng-invalid-required" auto-complete="" placeholder="To :" required="" style="display: none;">

Notice that the generated tokenInput markup is not part of the directive. So the real question here is how do I encapsulate a jquery plugin that generates its own markup within an angularjs directive?

like image 280
Mark Kenny Avatar asked Aug 26 '13 11:08

Mark Kenny


1 Answers

Following up @JqueryGuru suggestion, perhaps you'd be interested in taking a look at a tags input directive I've implemented recently: ngTagsInput. It's 100% Angular code and has several configuration options. You can see some demos here.

like image 172
Michael Benford Avatar answered Oct 25 '22 01:10

Michael Benford