AngularJS:在数据库中嵌入角度表达式(AngularJS: embedding angular expressions in database)

编程入门 行业动态 更新时间:2024-10-19 21:26:17
AngularJS:在数据库中嵌入角度表达式(AngularJS: embedding angular expressions in database)

我有2个数据库表, 字母letterTemplates与Angularjs前端。

在letterTemplates.content列中,我有一个带有嵌入式角度表达式的html模板,如下所示:

<p> Dear {{letter.user.name}},</p>

在字母预览中,我显示letterTemplate.content,如下所示:

<div ng-bind-html="letterTemplate.content"></div>

返回以下输出:

Dear {{letter.user.name}},

如何执行Angular表达式?


如何使用ng-bind-html编译angularjs代码中概述的解决方案依赖于使用硬编码的Angular模板,而我需要从API响应中解析html和angular语句。

I have 2 database tables, letters and letterTemplates with an Angularjs frontend.

In letterTemplates.content column, I have a html template with an embedded angular expression like so:

<p> Dear {{letter.user.name}},</p>

And in the letters preview, I display the letterTemplate.content like so:

<div ng-bind-html="letterTemplate.content"></div>

which returns the following output:

Dear {{letter.user.name}},

How do I get the Angular expression to execute?


The solutions outlined in How to make ng-bind-html compile angularjs code relied on using hardcoded Angular templates, whereas I need to parse html and angular statements from an API response.

最满意答案

以下是我如何使用它:

步骤1.安装此指令: https : //github.com/incuna/angular-bind-html-compile

步骤2.在模块中包含该指令。

angular.module("app", ["angular-bind-html-compile"])

步骤3.在模板中使用该指令:

<div bind-html-compile="letterTemplate.content"></div>

输出=

Dear John,

为了参考,这是相关的指令:

(function () { 'use strict'; var module = angular.module('angular-bind-html-compile', []); module.directive('bindHtmlCompile', ['$compile', function ($compile) { return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { element.html(value); $compile(element.contents())(scope); }); } }; }]); }());

Here's how I got it to work:

Step 1. Install this directive: https://github.com/incuna/angular-bind-html-compile

Step 2. Include the directive in the module.

angular.module("app", ["angular-bind-html-compile"])

Step 3. Use the directive in the template:

<div bind-html-compile="letterTemplate.content"></div>

Output =

Dear John,

For reference sake, here's the relevant directive:

(function () { 'use strict'; var module = angular.module('angular-bind-html-compile', []); module.directive('bindHtmlCompile', ['$compile', function ($compile) { return { restrict: 'A', link: function (scope, element, attrs) { scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile); }, function (value) { element.html(value); $compile(element.contents())(scope); }); } }; }]); }());

更多推荐

本文发布于:2023-04-28 03:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329769.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   数据库中   角度   AngularJS   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!