Insert HTML into view from AngularJS controller

前端 未结 18 1783
Happy的楠姐
Happy的楠姐 2020-11-21 06:00

Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view?

This comes from a requirement to turn an

18条回答
  •  别那么骄傲
    2020-11-21 06:43

    I found that using ng-sanitize did not allow me to add ng-click in the html.

    To solve this I added a directive. Like this:

    app.directive('htmldiv', function($compile, $parse) {
    return {
      restrict: 'E',
      link: function(scope, element, attr) {
        scope.$watch(attr.content, function() {
          element.html($parse(attr.content)(scope));
          $compile(element.contents())(scope);
        }, true);
      }
    }
    });
    

    And this is the HTML:

    
    

    Good luck.

提交回复
热议问题