Angular $compile template for dynamic email

眉间皱痕 提交于 2019-12-05 04:30:24

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!