0

我正在使用 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);
}

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

4

2 回答 2

0

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);

此外,如果它不起作用,最好创建一个有助于调试的小 plunkr 或 fiddle。

于 2014-07-16T10:18:12.077 回答
0

好的,所以 formatSelection os 不期望 html,而是 jqlite/angular 对象。但它将其转换为字符串。所以一种解决方案是删除转换并仅附加给定元素。另一种解决方案是使用容器元素作为第二个参数调用 formatSelection:

formatSelection(state, container) {
  container.append($compile('bla')(scope));
}

不返回任何内容,只需附加您的结果。

于 2014-07-16T13:55:40.370 回答