Angular $编译模板为动态电子邮件 [英] Angular $compile template for dynamic email

查看:256
本文介绍了Angular $编译模板为动态电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载一个html模板与ng-repeats在其中,然后使用 $ compile 服务来编译它,并在电子邮件中使用编译的html。

I am trying to load a html template with ng-repeats in it and then use the $compile service to compile it and use the compiled html in an email.

问题.... Ok之前问我让我设置术语...
绑定占位符: {{customer.name} }
绑定值:'john doe'

The problem.... Ok before asking let me set terminology... binding placeholder: {{customer.name}} binding value: 'john doe'

使用 $ interpolate 我得到实际绑定值,但不能使用ng-repeats。

Using $interpolate i get the actual binding values but does not work with ng-repeats.

示例: var html = $ interpolate('< p> {{customer.name}}< / p>')($ scope)
返回: < p> john doe< / p>'
Ng重复无效

Example:var html = $interpolate('<p>{{customer.name}}</p>')($scope) Returns: '<p>john doe</p>' Ng-repeats do not work

使用 $ compile 我得到绑定占位符即 {{customer.name}} 但我需要的是绑定值 john doe'

Using $compile i get the bindings placeholders ie {{customer.name}} but what I need is the binding value 'john doe' .

示例: var html = $ compile('< p> {{customer.name}}< / p>')范围
返回:'< p> {{customer.name}}< / p>'

一旦我添加到页面,我看到绑定值。但这是针对电子邮件不是一个页面,它有重复的 $ compile 可以编译

Once I append to a page I see the binding values. But this is for email not for a page plus it has ng-repeats that $compile can compile

如何我创建一个动态电子邮件模板,编译后,它返回html绑定值,而不只是绑定的占位符,所以我可以发送电子邮件?

How can I create a dynamic email template that after compiling it, it returns html with binding values and not just the binding placeholders so I can send it as email?

推荐答案

使用$ compile是正确的方法。但是, $ compile(template)($ scope)不会产生您期望的内插HTML。它只是将编译的模板链接到要在下一个 $ digest 期间内插的范围。要获得所需的HTML,您需要等待插值发生,如下所示:

Using $compile is the right way to go. However, $compile(template)($scope) doesn't yield the interpolated HTML that you expect right away. It just links the compiled template to a scope to be interpolated during the next $digest. To get the desired HTML, you need to wait for that interpolation to happen, like so:

var factory = angular.element('<div></div>');
factory.html('<ul ng-repeat="...">...</ul>');
$compile(factory)($scope);

// get the interpolated HTML asynchronously after the interpolation happens
$timeout(function () {
  html = factory.html();
  // ... do whatever you need with the interpolated HTML
});

(工作CodePen示例:http://codepen.io/anon/pen/gxEfr?editors=101

(working CodePen example: http://codepen.io/anon/pen/gxEfr?editors=101)

这篇关于Angular $编译模板为动态电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆