【问题标题】:Angular ui-select. Add choice separately from choices-repeat角度用户界面选择。与选择-重复分别添加选择
【发布时间】:2015-06-29 02:34:24
【问题描述】:

我需要在 ui-select 中显示一系列选项和一个自定义项。如何将此自定义项添加到当前指令?我无法在数组中插入此自定义项,因为显示此自定义项的 UI 不同

【问题讨论】:

  • 为什么要投反对票?有什么意见吗?

标签: angularjs ui-select


【解决方案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>

【讨论】:

  • 我添加了这段代码,但它似乎不起作用,所选元素从浏览器控制台正确附加,但不是从指令,stange
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
相关资源
最近更新 更多