可能将子组件放入 Angular 组件中。

huangapple go评论86阅读模式
英文:

Angular possible put child to component

问题

我可能有一个微不足道的问题。我有我的组件(app-comp)

<div class="test">
  <ng-content select="content1"></ng-content>
  <ng-content select="content2"></ng-content>
</div>

我在我的组件2中使用这个组件。

<div>
  <div content1>Test</div>
</div>

这里是否有任何选项可以像这样使用 app-comp:

<div>
  <content1>Test</content1>
</div>

这看起来更好,一目了然。也许我不能这样做,如果是这样的话,我在这里问的可能是无稽之谈,如果是这样,我道歉。但我有一种感觉,我以前见过这个,但我无法完全找到它,因为我不知道应该如何准确地称呼它。我明白这可能无法使用,因为组件是这样使用的。另一方面,我在想它可能在另一个组件内部,所以可能可以。非常感谢。

英文:

i have maybe trivial question. I have my component (app-comp)

&lt;div class=&quot;test&quot;&gt;
  &lt;ng-content select=&quot;content1&quot;&gt;&lt;/ng-content&gt;
  &lt;ng-content select=&quot;content2&quot;&gt;&lt;/ng-content&gt;
&lt;/div&gt;

I use this component in my component2.

&lt;div&gt;
  &lt;div content1&gt;Test&lt;/div&gt;
&lt;/div&gt;

Is here any option use app-comp like this:

&lt;div&gt;
 &lt;content1&gt;Test&lt;/content1&gt;
&lt;/div&gt;

It looks much better and it's clear at a glance. Maybe I can't and I'm asking nonsense here in which case I apologize. But I had a feeling I've seen this before and I can't quite find it because I don't know what exactly to call it. I understand that it might not be usable because that's how components are used. On the other hand, I was thinking it's inside another component, so it could be. Thank you very much.

答案1

得分: 1

第一和第二个块是正常的,不能按照你想要的方式缩短。

你在第三个块中使用的语法实际上是用来调用组件的。所以这样做:

&lt;content1&gt;Test&lt;/content1&gt;

将调用具有 'content1' 作为选择器的组件。

这是两个不同的事情,不应混淆。

此外,写成

&lt;div content1&gt;Test&lt;/div&gt;

已经是一种相当酷和直接的方式在组件中注入 HTML 了 可能将子组件放入 Angular 组件中。

英文:

The first and second blocks are fine and cannot be shortened the way you want.

The syntax you are using in the third block is actually the one used to call a component. So doing:

&lt;content1&gt;Test&lt;/content1&gt;

Will call the component with 'content1' as selector.

It is two differents things that shall not be confused.

Plus, writting

&lt;div content1&gt;Test&lt;/div&gt;

Is already a pretty cool and straight forward way to inject html in a component 可能将子组件放入 Angular 组件中。

huangapple
  • 本文由 发表于 2023年6月2日 14:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定