纯粹在 HTML 中声明 Angular 指令?

Declare Angular Directives purely in HTML?

(与我在 Programmers.SE 上的另一个问题高度相关: https://softwareengineering.stackexchange.com/questions/280249/whether-to-abstract-small-repeating-code-segments-in-html-templates)

当我想重用几行 HTML 时,我不得不在另一个文件中编写一些 JavaScript 样板文件来创建指令,这真的很烦人。我真的很想在同一个 HTML 文件中创建一个标记指令。

然后我想到了一个主意:我可以创建一个特殊的指令(下面的 custom-tag)来声明来自 HTML 的指令。

例如:

<custom-tag name="icon" params="{which: '@which'}">
    <span class="glyphicon glyphicon-{{which}}" />
</custom-tag>

这将翻译成一些 JavaScript,例如:

module.direcrtive('icon', {
restrict: 'E',
scope = {which: '@which'},
template = '<span class="glyphicon glyphicon-{{which}}" />',
});

我可以这样称呼它

<icon which="asterisk" />

我的问题是 Angular 中是否已经存在类似的东西?

(我知道这只是重新发明了一些其他的模板框架,但我必须使用 Angular。)

在 angular 中有 (afaik) 3 种引用模板的方法:

  • 正如问题中已经提到的,可以将标记直接放在 template 属性

  • 模板可以通过 templateId 在单独的文件中引用

  • 可以在<script type="text/ng-template" id="template_id.html"> .... </script>的同一页面内定义模板。 并通过 templateId: "template_id.html" 引用。有关此的更多详细信息:Using Inline Templates in Angular