英文:
How ng-content will be using in new release angular v16
问题
在以前的版本中,ng-content的工作方式如下:
<app-component>
<ng-content></ng-content>
</app-component>
但在Angular v16中,这种声明方式已经更改为:
<app-component />
所以,我的问题是要了解在新的方法中如何继续使用ng-content。
我想知道在Angular v16中如何使用ng-content。
英文:
In previous version, ng-content works like this:
<app-component>
<ng-content></ng-content>
</app-component>
But in angular v16, this approach of declaring has changed to <app-component />
So, my question is to know how we will be still using ng-content in new approach
I would like to know how we can use ng-content in angular v16
答案1
得分: 3
根据发布博客,您可以使用自闭标签,但不是必须的。这只是一个便利功能。如果在组件上使用内容投影,在使用该组件时不能使用自闭标签。因此,关闭组件标签的"旧"方式仍然有效,甚至在某些情况下是必要的。
在您的情况下,不能对app-component
使用自闭标签,但可以对ng-content
使用,因为后者不使用内容投影:
<app-component>
<ng-content />
</app-component>
英文:
According to the release blog, you can use self closing tags, but you don't need to. It's just a convenience feature. If you use content projection on a component, you cannot use self-closing tags when using this component. So the "old" way of closing the component's tag is still valid and in some cases even necessary.
In your case you cannot use a self-closing tag for the app-component
, but for the ng-content
, as the latter doesn't use content projection:
<app-component>
<ng-content />
</app-component>
答案2
得分: 0
自闭合标签在没有包含内容时充当语法快捷方式。因此,要使用ng-content
,您应该保持旧的方式。
英文:
Self-closing tags serve as a syntactic shortcut when there is no content enclosed within them.
So in order to use ng-content
, you should keep the old way
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论