将文件中的 HTML 模板加载到 AngularJs 中的变量中

将文件中的 HTML 模板加载到 AngularJs 中的变量中

问题描述:

我正在处理一个需要将 HTML 绑定到富文本编辑器的表单.存储此 HTML 内容的最佳方式是 HTML 文件.

I'm working with a form that needs to bind HTML to a Rich Text Editor. The best way to store this HTML content would be an HTML file.

我不太明白如何从文件加载 HTML 模板并将其分配给变量.

I can't quite figure out how to load an HTML template from a file and assign it to a variable.

当使用 templateUrl 时,指令似乎确实能够做到这一点.想知道这是否有任何低级的 api 角度可以在控制器内部实现相同的功能

Directives seem to do be able to do this when working with templateUrl. Was wondering if this there is any low level api in angular to achieve the same thing inside of a controller

所有模板都加载到缓存中.有一个可注入的 $templateCache 服务可用于访问模板:

All templates are loaded into a cache. There is an injectable $templateCache service you can use to get access to the templates:

app.controller('testCtrl', function($scope, $templateCache){
    var template = $templateCache.get('nameOfTemplate.html');
});