Insert HTML into view from AngularJS controller

前端 未结 18 1775
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:37

    Angular JS shows HTML within the tag

    The solution provided in the above link worked for me, none of the options on this thread did. For anyone looking for the same thing with AngularJS version 1.2.9

    Here's a copy:

    Ok I found solution for this:

    JS:

    $scope.renderHtml = function(html_code)
    {
        return $sce.trustAsHtml(html_code);
    };
    

    HTML:

    EDIT:

    Here's the set up:

    JS file:

    angular.module('MyModule').controller('MyController', ['$scope', '$http', '$sce',
        function ($scope, $http, $sce) {
            $scope.renderHtml = function (htmlCode) {
                return $sce.trustAsHtml(htmlCode);
            };
    
            $scope.body = '
    '; }]);

    HTML file:

提交回复
热议问题