Search code examples
ng-tags-input

How to access leftover text in ng-tags-input


Is the left over text in input accessible programatically? If so, how?

I only allow tags from autocomplete (to use as search filters), and want to use the left over text as additional keywords, meaning I want to know if it's bound to anything so I can pass it to a search function.

Thanks for the help


Solution

  • That's not directly possible, but you can hack into the directive and make it work by using a helper directive:

    app.directive('bindInternalInputTo', function() {
      return function(scope, element, attrs) {
        var property = attrs.bindInternalInputTo,
            input = element.find('input'),
            inputScope = input.scope();
    
        inputScope.$watch('newTag.text', function(value) {
          scope[property] = value;
        });
      };
    });
    

    Now you can bind some variable in the outer scope to the inner input by doing the following:

    <tags-input ng-model="tags" bind-internal-input-to="variable"></tags-input>
    

    Working Plunker

    Please note that this solution isn't guaranteed to work with future versions of ngTagsInput since it relies on internal implementation details.