简体   繁体   English

$ http响应中的Angular指令

[英]Angular directives in $http response

Angular 1.5 Angular 1.5

My $http data service returns html encoded text with directives too, like ng-click in the text. 我的$ http数据服务也返回带有指令的html编码文本,例如文本中的ng-click。

I need to display the html and have the ng-click directives get activated. 我需要显示html并激活ng-click指令。

To display I am doing this and it works, but ng-clicks don't work: 要显示我正在执行此操作并且它有效,但ng-clicks不起作用:

 <div class="mt10" ng-repeat="row in aqdas.Paragraphs" ng-cloak>
    <span  ng-bind-html="TrustDangerousSnippet(row.Text)" >
         {{row.Text}}
    </span>
</div>

Here is TrustDangerousSnippet: 这是TrustDangerousSnippet:

    $scope.TrustDangerousSnippet = function (text) {
        var val = $sce.trustAsHtml(text);
        return val;
    };

How can I edit TrustDangerousSnippet so that the ng-click's in the text are turned on once $http downloads the code? 我如何编辑TrustDangerousSnippet,以便在$ http下载代码后打开文本中的ng-click?

Use this Directive also with your code. 将此指令也与您的代码一起使用。 to bind html element in directive use complie. 在指令中绑定html元素使用complie。 it will work.. 它会工作..

.directive('compile', ['$compile', function ($compile) {
    return function(scope, element, attrs) {
      scope.$watch(
        function(scope) {
           return scope.$eval(attrs.compile);
        },
        function(value) {
           element.html(value);
           $compile(element.contents())(scope);
        }
    );
  };
}])

I added the Directive Suresh included and changed the HTML to look like this, it works now. 我添加了包含Suresh的指令,并将HTML更改为这样,现在可以使用了。 (add 'compile' to the binding element) (将'compile'添加到绑定元素)

<div class="mt10" ng-repeat="row in aqdas.Paragraphs" ng-cloak>
<span  compile ng-bind-html="TrustDangerousSnippet(row.Text)" >
     {{row.Text}}
</span>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM