Insert HTML into view from AngularJS controller

前端 未结 18 1785
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:25

    You can also create a filter like so:

    var app = angular.module("demoApp", ['ngResource']);
    
    app.filter("trust", ['$sce', function($sce) {
      return function(htmlCode){
        return $sce.trustAsHtml(htmlCode);
      }
    }]);
    

    Then in the view

    Note: This filter trusts any and all html passed to it, and could present an XSS vulnerability if variables with user input are passed to it.

提交回复
热议问题