ui-select の選択肢の配列と、もう 1 つのカスタム項目を表示する必要があります。このカスタム項目を現在のディレクティブに追加するにはどうすればよいですか? このカスタム アイテムを表示するための UI が異なるため、このカスタム アイテムを配列に挿入できません。
2326 次
1 に答える
1
解決策が見つかりました:項目リストの下部に手動で追加する ui-select 要素にカスタム ディレクティブを追加しました。
コード (トピックで説明されているよりもいくつかの追加の属性と機能が含まれています。そのまま投稿しました):
.directive('customOption', function($timeout){
return {
restrict: 'A',
link: function(scope, el, attrs){
var options = JSON.parse(attrs.customOption);
var buttonText = options.buttonText;
var func = options.click;
var type = options.type;
var condition = options.showIf;
var template = "<div class='custom-option-container'><button type='button' class='btn btn-primary'><span class='glyphicon glyphicon-plus-sign'></span>" + buttonText + "</button></div>";
//if condition evaluated to true or no condition
if(scope.$eval(condition) || !condition){
el.find('li.ui-select-choices-group').append(template);
el.find('ul.ui-select-choices').removeAttr('ng-show');
//watch and
//remove ng-hide class to display this custom item even if there are no more items
scope.$watch(function(){
return el.find('ul.ui-select-choices').hasClass('ng-hide');
}, function(newVal){
if(newVal){
$timeout(function() {
el.find('ul.ui-select-choices').removeClass('ng-hide');
});
}
});
el.find('div.custom-option-container button').bind('click', function(){
scope[func].apply(null, [type]);
})
};
}
};
})
HTML:
<ui-select ng-model="asset.category" custom-option='{"buttonText": "Add New Category", "click" : "showAddModal", "type": "category"}'>
<ui-select-match>{{ $select.selected.categoryName }}
</ui-select-match>
<ui-select-choices repeat="category in categories | filter: $select.search">
<span ng-bind-html="category.categoryName | highlight: $select.search"></span>
</ui-select-choices>
</ui-select>
于 2015-04-22T09:00:53.773 に答える