angularJS How can I ignore certain HTML tags?

前端 未结 3 1802
暖寄归人
暖寄归人 2020-12-31 19:01

I got this error because one of the users added in his post <3

Error: [$sanitize:badparse] The sanitizer was unable to parse the foll

3条回答
  •  攒了一身酷
    2020-12-31 19:37

    To preserve existing ng-bind-html behaviour without crashing you can catch the $sanitize:badparse exception.

    The ngBindHtml component internally uses the ngSanitize service. Inject $sanitize into your controller and catch it.

    The advantage of this versus the $sce.trustAsHtml methods is that $sanitize does not introduce any potential security holes (eg. javascript injection).

    Controller (inject $sanitize):

    $scope.clean = function (string) {
        $scope.clean = function(string) {
            try {
                return $sanitize(string);
            } catch(e) {
                return;
            }
        };
    };
    

    This method could be improved with a cache of the last known good value.

    View:

提交回复
热议问题