0

I'm trying to dynamically load pop over on lable my code was

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

my html was

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

problem is html code is working but popover not working. may i know the problem with fix.

thanks

UPDATED

OK guys here is the Plunker

Gayan
  • 2,750
  • 5
  • 47
  • 88

1 Answers1

0

Here is some workaround: 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);
            });
          }
        }
      });

Actually it is similiar question, as I said in comments. So you need to compile HTML one more time after inserting HTML into tag.

So I have create directive compileHtml you should pass function which returns sanitized HTML (message-fn in code) and also provide params for that function (message in code). Directive watch for changes of message update HTML and re-run compile.

UPDATE

Here is fork of your plnkr

In my humble opinion, it is better to create directive which will $watch over message, sanitize it without any external function and re-compile.

Community
  • 1
  • 1
Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30
  • can't we compile HTML on controller – Gayan Oct 23 '14 at 08:26
  • You can compile on controller, but it is not good practice to $watch and work with HTML element in controller. – Ruben Nagoga Oct 23 '14 at 08:37
  • OK that'll be acceptable with few changes message attribute actually not need. we can have it without it. here is the updated one. http://plnkr.co/edit/Y3z4ce5s8YorADjgS2mx?p=preview – Gayan Oct 23 '14 at 08:56