2015-06-23 46 views
0

我使用ngTagsInput,包括autosuggest-function,如果用戶點擊側邊欄菜單(除了直接輸入),我還希望添加帶有點擊事件的標籤。所有現有的標籤應該仍然存在。我目前使用一個可怕的黑客。當用戶點擊第一個鏈接並點擊第二個鏈接時,一切正常。在鏈接3中,鏈接1和鏈接2在一天內彙總。感謝您的竅門ngTagsInput-在函數中添加標籤

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]; 
    }; 

}; 

回答

0

O.k.我找到了一個解決方案:

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