简体   繁体   English

更改$ compile对象的html在Angular中失败

[英]Changing the html of a $compile's object fails in Angular

Angular 1.5: I have an ng-repeat which renders html that has ng-clicks in the text. Angular 1.5:我有一个ng-repeat,它渲染文本中具有ng-clicks的html。 Works fine. 工作正常。

Here's my compile directive: 这是我的编译指令:

app.controller('PageCtrl', pageController)
    .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);
                    }
                );
            };
        }
    ]);

Here's my html-bind with the compile directive 这是我的html-bind与compile指令

<div  class="mt10" ng-repeat="row in aqdas.Notes  | filter: filterNotes">
    <span class="pull-left text-bold mr5">{{row.Number}}. </span>
      <div compile ng-bind-html="TrustDangerousSnippet(row.Text,'note')">
           {{row.Text}}    
    </div>
</div>

Here's my TrustDangerousSnippet (simplified) 这是我的TrustDangerousSnippet(简体)

    $scope.TrustDangerousSnippet = function(text, kind) {
        var simpletext = new RegExp("(" + $scope.search + ")", "gi");
        text = text.replace(simpletext, "<mark>$1</mark>");

        var val = $sce.trustAsHtml(text);
        return val;
    };

Problem: In the TrustDangerousSnippet I use a reg ex to highlight an area of the text. 问题:在TrustDangerousSnippet中,我使用正则表达式突出显示文本区域。 When I do, the compile doesn't work. 当我这样做时,编译不起作用。 It seems when I change the text being bound, the $compile doesn't like it/allow it? 似乎当我更改要绑定的文本时,$ compile不喜欢/允许它吗?

How can I either make the $compile like it, or some other way highlight the text? 如何使$ compile像这样,或以其他方式突出显示文本?

Maybe this? 也许这个吗?

<div compile="TrustDangerousSnippet(row.Text, 'note')"></div>

Directive expects parameter. 指令的期望参数。 Try like this and remove watch (do one time compile), you may have digest problem. 像这样尝试并删除手表(进行一次编译),您可能会遇到消化问题。

app.controller('PageCtrl', pageController)
.directive('compile', [
    '$compile', function($compile) {
        return function(scope, element, attrs) {
            var html = scope.$eval(attrs.compile);
            element.append($compile(html)(scope));
        };
    }
]);

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

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