Resolve template in AngularJS similar to Handlebars?

放肆的年华 提交于 2019-12-25 05:18:22

问题


How to resolve an AngularJS template with a syntax similar to Handlebar?

<script type="text/ng-template" id="mytemplate">
  Name is {{name}}
</script>

I can get the template using $templateCache.get('mytemplate') buthow to resolve that template to a valid HTML that can be inserted into DOM.

For example:

var html = $templateCache.get('mytemplate', {name: 'Ace' });

should output "Name is Ace"


回答1:


Disclaimer: I haven't tried this, but I think it will work.

var scope = $rootScope.$new();
scope.name = 'Ace';
var html = $compile($templateCache.get('myTemplate'))(scope);

Basically, create a Scope, set values on it and then pass it into a compiled template.



来源:https://stackoverflow.com/questions/25203125/resolve-template-in-angularjs-similar-to-handlebars

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