ngTagsInput-在函数中添加标记

时间:2015-06-23 05:56:39

标签: angularjs

我使用ngTagsInput包括autosuggest-function,如果用户点击侧栏菜单(除直接输入外),我还想用click事件添加Tags。所有现有标签应该仍然存在。我目前使用了一个可怕的黑客。当用户单击第一个链接并单击第二个链接时,一切正常。在链接3中,链接1和链接在一天内汇总。谢谢你的Tipps

HTML标记输入

<tags-input class="bootstrap" id="tagsInput"
        placeholder=""
        add-on-enter="true"
        addFromAutocompleteOnly="false"
        on-tag-added="tagAdded($tag)" 
        on-tag-removed="tagRemoved($tag)"
        ng-model="autosuggest"
        replace-spaces-with-dashes="false">
  <auto-complete source="loadAutosuggest($query)"
        select-first-match="false"
        min-length="0"
        debounce-delay="0"
        max-results="10">
  </auto-complete>
</tags-input>

带有链接的HTML边栏菜单

<ul class="nav nav-second-level collapse">
   <li ng-repeat="item in icdItems">
     <a href="#" ng-attr-title="item.title" ng-click="getTitle(item.title)"><span>{{ item.name }}</span></a>
   </li>
</ul>

JS

$scope.getTitle = function(title) {

    var currentVal = $scope.autosuggest;
    if (currentVal == '') {
        $scope.autosuggest = [title];
    } else {
        var autosuggest = $scope.autosuggest;
        var textOnly = autosuggest.map(function(el){
             return el.text;
           }).join(", ");
        console.log($scope.autosuggest);
        $scope.autosuggest = [textOnly, title];
    };

};

1 个答案:

答案 0 :(得分:0)

O.k。我找到了解决方案:

$scope.getTitle = function(title) { 
    var newTitle={'text':title};    
    $scope.autosuggest.push(newTitle);
    console.log($scope.autosuggest);
};