【问题标题】:Popover not working with ng-bind-html弹出框不适用于 ng-bind-html
【发布时间】:2014-10-14 04:59:26
【问题描述】:

我正在尝试在标签上动态加载弹出框,我的代码是

$scope.setStatusMessage = function (message) {
    var lblClass = "", status = "" , template;
        lblClass = "label label-danger";
        status = "failed";
    template = '<label popover="' + message + '" popover-trigger="mouseenter" class="' + lblClass + '">' + status + '</label>';
    return $sce.trustAsHtml(template);
}

我的html是

<span data-ng-bind-html="setStatusMessage(statusMessage)"></span>

问题是 html 代码可以正常工作,但 popover 不能正常工作。我可以知道修复的问题吗?

谢谢

更新

好的,伙计们,这里是Plunker

【问题讨论】:

  • 您的浏览器控制台有错误吗?
  • 没什么。在我的浏览器控制台上。
  • 你能创建任何小提琴以便更好地理解
  • Ruben Nagoga - 不,伙计,这不是

标签: angularjs angular-ui-bootstrap popover


【解决方案1】:

这里有一些解决方法:http://plnkr.co/edit/pferF3otkczQo9TJbKck?p=preview

directive('compileHtml', function($compile) {
        return {
          restrict: 'A',
          scope: {
            messageFn: '&',
            message: '='
          },
          link: function(scope, iElement, iAttrs) {
            scope.$watch('message', function(message) {
              var html = scope.messageFn({'message': message});
              iElement.html(html);
              $compile(iElement.contents())(scope);
            });
          }
        }
      });

其实和question类似,就像我在cmets里说的那样。所以你需要在将 HTML 插入标签之后再编译一次 HTML。

所以我创建了指令compileHtml,你应该传递返回净化HTML的函数(代码中的message-fn)并为该函数提供参数(代码中的message)。指令监视 message 的变化更新 HTML 并重新运行编译。

更新

这是你的plnkr的分支

以我的拙见,最好创建将$watch 传递给消息的指令,在没有任何外部函数的情况下对其进行清理并重新编译。

【讨论】:

  • 我们不能在控制器上编译 HTML
  • 您可以在控制器上编译,但在控制器中使用 $watch 和 HTML 元素并不是一个好习惯。
  • 好的,这将是可以接受的,实际上不需要更改消息属性。我们可以没有它。这是更新的。 plnkr.co/edit/Y3z4ce5s8YorADjgS2mx?p=preview
猜你喜欢
  • 2014-02-06
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2017-12-26
  • 1970-01-01
相关资源
最近更新 更多