AngularJs - 从编译的 HTML/变量中获取一个字符串

AngularJs - Get a string from compiled HTML/Variable

本文关键字:获取 字符串 一个 变量 编译 HTML AngularJs      更新时间:2023-09-26

我尝试从来自服务器(在数据库中)的模板中获取一个字符串,以便以后可以对其进行编辑(使用 Textangular 或将其保存在数据库中)。

    TemplateMailService.get($scope.type, function (data, status) {

            $scope.template = data.template;
            // data.template.content = '<p>Hi <span ng-if="client.email == 'email@email.com'">{{email}}</span>!</p>'
            $timeout(function () {
                data.template.content = $compile(data.template.content)($scope);
                $log.debug('$scope.mail.content : ', $scope.mail.content);
                $log.debug('data.template.content : ', angular.element(data.template.content));
                // is compiled correctly. I can see the ng-if apply and the {{variable}} compiled
                var string = '';
                string += data.template.content[0].innerHTML;
                console.log(string);
                // => ng-if not apply and {{variable}} not compiled...
                $scope.mail.content = string;
            }, 0);
    })

我希望能够保存编译的模板(将所有 ng-xx 和 {{ 变量}} 应用于字符串中以文本角度编辑它。

我知道我应该使用指令执行此操作,但我不明白如何在字符串中返回编译的模板。

我在这里错过了什么?

谢谢。

$parse

$parse可用于计算单个表达式,就像data.template.content[0].innerHTML;字符串中的任何{{variable}}一样。

$interpolate

我认为这是获取已编译字符串的解决方案,因为$interpolate知道如何使用<p ng-if='myObject'>Hello {{myName}}</p>