英文:
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)
<div class="test">
<ng-content select="content1"></ng-content>
<ng-content select="content2"></ng-content>
</div>
I use this component in my component2.
<div>
<div content1>Test</div>
</div>
Is here any option use app-comp like this:
<div>
<content1>Test</content1>
</div>
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
第一和第二个块是正常的,不能按照你想要的方式缩短。
你在第三个块中使用的语法实际上是用来调用组件的。所以这样做:
<content1>Test</content1>
将调用具有 'content1' 作为选择器的组件。
这是两个不同的事情,不应混淆。
此外,写成
<div content1>Test</div>
已经是一种相当酷和直接的方式在组件中注入 HTML 了
英文:
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:
<content1>Test</content1>
Will call the component with 'content1' as selector.
It is two differents things that shall not be confused.
Plus, writting
<div content1>Test</div>
Is already a pretty cool and straight forward way to inject html in a component
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论