tokenInput为angular.js指令

Mar*_*nny 7 jquery-tokeninput angularjs angularjs-directive

我正在尝试从James Smith的tokenInput Jquery插件创建一个angular.js指令:http://loopj.com/jquery-tokeninput

这是我到目前为止:

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
            });
            }
    };
});
Run Code Online (Sandbox Code Playgroud)

使用以下标记:

<input type="text" name="recipient" ng-model="conversation.recipients" class="messageComposeTextField" auto-complete placeholder="To :" required />
Run Code Online (Sandbox Code Playgroud)

TokenInput工作正常但我的问题是我无法绑定到模型.

{{conversation.recipients}} 
Run Code Online (Sandbox Code Playgroud)

是空白的.

tokenInput插件使用列表元素(ul和li)生成它自己的标记.因此,在检查元素后,我得到:

<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;">
Run Code Online (Sandbox Code Playgroud)

请注意,生成的tokenInput标记不是指令的一部分.所以真正的问题是如何封装一个jquery插件,在一个angularjs指令中生成自己的标记?

Mic*_*ord 10

跟随@JqueryGuru的建议,也许你有兴趣看看我最近实现的标签输入指令:ngTagsInput.它是100%Angular代码,有几个配置选项.你可以在这里看到一些演示.


JQu*_*uru 3

我建议您使用 ui-select2 即可使用指令来实现类似的功能@ https://github.com/angular-ui/ui-select2,它提供了与您的要求类似的“简单标记模式”

检查新示例旧的例子可以在这里找到。

$scope.tagsSelection = [
    { "id": "01", "text": "Perl" },
    { "id": "03", "text": "JavaScript" }
];       

$timeout(function(){
    $scope.tagsSelection.push({ 'id': '02', 'text': 'Java' });
}, 3000);

$scope.tagData = [
    {
        "id": "01",
        "text": "Perl"
    },
    {
        "id": "02",
        "text": "Java"
    },
    {
        "id": "03",
        "text": "JavaScript"
    },
    {
        "id": "04",
        "text": "Scala"
    }
];

$scope.tagAllOptions = {
    multiple: true,
    data: $scope.tagData
};
Run Code Online (Sandbox Code Playgroud)

您可以在以下位置查看选项和文档的详细信息: http: //ivaynberg.github.io/select2/