忽略作用域变量的清理

Ignore sanitization for a scope variable

本文关键字:变量 作用域      更新时间:2023-09-26

我正在尝试实现一个文本编辑器,该编辑器可以使用Angular和TinyMCE进行html输入。问题是 - 如果我使用 ngModel 绑定,当我在该模型变量中有 mathml 标签时,它们就会被剥离。是否可以完全忽略作用域变量的清理?我所说的消毒,我的意思是这个 - https://docs.angularjs.org/api/ngSanitize/service/$sanitize

这是我正在做的:(供参考)

<script>
        appControllers.controller('appController',['$scope','$sce',
        function($scope, $sce){
            $scope.tinymceOptions = {
                height: '450px',
                statusbar: true,
            };
            $scope.editorText = "<math xmlns:mml='http://www.w3.org/1998/Math/MathML' xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math'><mi>H</mi><mi>C</mi><mo>≡</mo><mi>C</mi><mo>-</mo><mtable><mtr><mtd><mtable><mtr><mtd><mi>C</mi><msub><mrow><mi>H</mi></mrow><mrow><mn>3</mn></mrow></msub></mtd></mtr><mtr><mtd><mo>|</mo></mtd></mtr><mtr><mtd><mi>C</mi></mtd></mtr></mtable></mtd></mtr><mtr><mtd><mo>|</mo></mtd></mtr><mtr><mtd><mi>C</mi><msub><mrow><mi>H</mi></mrow><mrow><mn>3</mn></mrow></msub></mtd></mtr></mtable><mo>-</mo><msub><mrow><mi>C</mi><mi>H</mi></mrow><mrow><mn>3</mn></mrow></msub></math>";
            $scope.deliberatelyTrustDangerousSnippet = function(){
                return $sce.trustAsHtml($scope.editorText);
            };
        }]);
    </script>
<textarea ui-tinymce="tinymceOptions" ng-model="editorText"></textarea>
<p>{{deliberatelyTrustDangerousSnippet()}}</p>

我忘记了我以前为什么这样做,但我遇到了类似的事情,这对我有帮助。

app.config(function ( $sceProvider) {
    $sceProvider.enabled(false);        
});

请注意,这将适用于整个应用程序,因此在使用诸如ng-bind-html之类的东西时会增加风险