Using ng-bind-html and $sce.trustAsHtml create a string with ng-model binding

后端 未结 1 1882
悲哀的现实
悲哀的现实 2020-12-21 10:15

I want to create dynamically forms. Inside my controller i create a string

var str = \"

        
1条回答
  •  春和景丽
    2020-12-21 11:08

    HTML :

    Add a directive: compile-template

    JS :

    angular.module('ngApp', ['ngSanitize'])
    .controller('controller1', ['$scope','$sce', function($scope, $sce) {
        var str = "";
        $scope.htmlString = $sce.trustAsHtml(str);
    }])
    .directive('compileTemplate', function($compile, $parse){
        return {
            link: function(scope, element, attr){
                var parsed = $parse(attr.ngBindHtml);
                function getStringValue() {
                    return (parsed(scope) || '').toString();
                }
    
                // Recompile if the template changes
                scope.$watch(getStringValue, function() {
                    $compile(element, null, -9999)(scope);  // The -9999 makes it skip directives so that we do not recompile ourselves
                });
            }
        }
    });
    

    0 讨论(0)
提交回复
热议问题