在Angular js中调用$ sce.trustAsHtml()字符串中的函数

时间:2021-11-22 12:45:21

I am developing an app using Angularjs and adding HTML using $sce.trustAsHtml() in my page. I want to call a function in above dynamically added content. My html and script as below.

我正在使用Angularjs开发一个应用程序,并在我的页面中使用$ sce.trustAsHtml()添加HTML。我想在上面动态添加的内容中调用一个函数。我的html和脚本如下。

HTML

<div ng-app="ngBindHtmlExample">
  <div ng-controller="ngBindHtmlCtrl">
   <p ng-bind-html="myHTML"></p>
  </div>
</div>

Javascript

angular.module('ngBindHtmlExample', ['ngSanitize'])

.controller('ngBindHtmlCtrl', ['$scope','$sce', function ngBindHtmlCtrl($scope, $sce) {
  $scope.myHTML =$sce.trustAsHtml(
     'I am an <code>HTML</code>string with <a href="#" ng-mouseover="removeExp()">links!</a> and other <em>stuff</em>');

    $scope.removeExp = function (){
       console.log('dfdfgdfgdfg');
    }
}]);

jsfiddle

Click Here to see

点击这里查看

1 个解决方案

#1


47  

It's a bit tricky because ng-bind-html will simply insert plain old html and not bother compiling it (so any directives in the html will not be processed by angular.

这有点棘手,因为ng-bind-html只会插入普通的旧html而不打算编译它(所以html中的任何指令都不会被angular处理。

The trick is finding a way to compile whenever the template changes. For example, you could create a directive that does this. It would look something like:

诀窍是找到一种在模板更改时进行编译的方法。例如,您可以创建一个执行此操作的指令。它看起来像:

.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
            });
        }         
    }
});

You can then use it like this:

然后你可以像这样使用它:

<p ng-bind-html="myHTML" compile-template></p>

See the working example here:

请参阅此处的工作示例:

http://jsfiddle.net/3J25M/2/

http://jsfiddle.net/3J25M/2/

#1


47  

It's a bit tricky because ng-bind-html will simply insert plain old html and not bother compiling it (so any directives in the html will not be processed by angular.

这有点棘手,因为ng-bind-html只会插入普通的旧html而不打算编译它(所以html中的任何指令都不会被angular处理。

The trick is finding a way to compile whenever the template changes. For example, you could create a directive that does this. It would look something like:

诀窍是找到一种在模板更改时进行编译的方法。例如,您可以创建一个执行此操作的指令。它看起来像:

.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
            });
        }         
    }
});

You can then use it like this:

然后你可以像这样使用它:

<p ng-bind-html="myHTML" compile-template></p>

See the working example here:

请参阅此处的工作示例:

http://jsfiddle.net/3J25M/2/

http://jsfiddle.net/3J25M/2/