首页 > 解决方案 > 为什么,何时以及如何在 Angular 中使用这个 sintaxis?

问题描述

我正在寻找有关依赖注入的信息,然后我找到了这篇文章:

https://medium.com/angular-chile/inyecci%C3%B3n-de-componentes-y-directivas-en-angular-6ae75f64be66

在那里我看到了这个语法

   <ui-card>
  <h1>Your daily @agadmator quote</h1>
  <p>Congratulations! You are an excellent analyzer of hypothetical end game positions and that never actually happened.</p>
</ui-card>

它引起了我的注意。

我相信自定义角度标签(自定义标签选择器)之间没有任何关系。

我想了解有关它的更多信息,但我不知道要使用哪些搜索词以及该语法是如何工作的。

你能帮我解释或分享有关它的信息吗?

标签: javascriptangulardependency-injectionsyntaxdocumentation

解决方案


这就是所谓的“内容投影”(以前称为“嵌入”)。

查看有关如何在此处使用的教程:https ://scotch.io/tutorials/angular-2-transclusion-using-ng-content

简而言之,要将数据传递给您的组件,您通常会这样做:

<my-card [content]="myVar"></my-card>

但是,当您需要传递其他 HTML 元素时,可以使用内容投影:

<my-card>
    <h1>Hello world</h1>
</my-card>

在里面,MyCardComponent您可以使用特殊标签访问该内容<ng-content>


推荐阅读