簡體   English   中英

$ http響應中的Angular指令

[英]Angular directives in $http response

Angular 1.5

我的$ http數據服務也返回帶有指令的html編碼文本,例如文本中的ng-click。

我需要顯示html並激活ng-click指令。

要顯示我正在執行此操作並且它有效,但ng-clicks不起作用:

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

這是TrustDangerousSnippet:

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

我如何編輯TrustDangerousSnippet,以便在$ http下載代碼后打開文本中的ng-click?

將此指令也與您的代碼一起使用。 在指令中綁定html元素使用complie。 它會工作..

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

我添加了包含Suresh的指令,並將HTML更改為這樣,現在可以使用了。 (將'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