Angularjs ui-select2格式选择格式到指令

Angularjs ui-select2格式选择格式到指令,angularjs,angularjs-directive,ui-select2,Angularjs,Angularjs Directive,Ui Select2,我正在使用ui-select2添加多个项目(标记模式)。当添加一个项目时,我想在输入中显示一些特殊的ui,所以我想添加一些指令。 正在尝试使用formatSelection格式化输入: function colorFormat(state) { return '<color-selection selections="option.selections"></color-selection>'; } 函数颜色格式(状态){ 返回“”; } 但该指令并未编译。所以

我正在使用ui-select2添加多个项目(标记模式)。当添加一个项目时,我想在输入中显示一些特殊的ui,所以我想添加一些指令。 正在尝试使用formatSelection格式化输入:

function colorFormat(state) {
   return '<color-selection selections="option.selections"></color-selection>';
}
函数颜色格式(状态){
返回“”;
}
但该指令并未编译。所以我编译了它:

function colorFormat(state) {
  return $compile('<color-selection selections="option.selections"></color-selection>')(scope);
}
函数颜色格式(状态){
返回$compile(“”)(范围);
}

但是现在,这个值是['object']。看来格式正在串接我的结果。那个么如何格式化为指令呢?

formatSelection要求返回html,而$compile服务将返回对象。 在我看来,您需要编译元素并获得html字符串

试试这个

  var compiledHTML = $compile('<color-selection selections="option.selections"></color-selection>')(scope);
    $timeout(function(){

        var formatString = '';
        for(i=0; i<compiledHTML.length; i++)
            formatString += compiledHTML[i].outerHTML;

        return formatString ;
    },0);
var compiledHTML=$compile(“”)(范围);
$timeout(函数(){
var formatString='';

对于(i=0;iok),formatSelection操作系统不需要html,而是jqlite/angular对象。但它将其转换为字符串。因此,一种解决方案是删除转换并仅附加给定元素。 另一种解决方案是使用容器元素作为第二个参数调用formatSelection:

formatSelection(state, container) {
  container.append($compile('bla')(scope));
}
不返回任何内容,只需附加结果