英文:
Embed contents of angular component directly, without wrapper
问题
现在让我们假设我有一个像这样的网格:
<div style="display:grid; grid-template-columns auto auto;">
<div>Row 1 Col 1</div><div>Row 1 Col 2</div>
<div>Row 2 Col 1</div><div>Row 2 Col 2</div>
</div>
这将显示如下:
|Row 1 Col 1|Row 1 Col 2|
|Row 2 Col 1|Row 2 Col 2|
完美,正是我想要的。
现在,假设我想重构我的代码,使每个**行**都是一个 Angular 组件
<div style="display:grid; grid-template-columns auto auto;">
<app-row-component row=1></app-row-component>
<app-row-component row=2></app-row-component>
</div>
`app-row-component` 看起来像这样:
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
现在,网格的样式不再起作用,因为 Angular 将 `app-row-component` 的内容包装在自己的元素中,网格看起来像这样:
|Row 1 Col 1 Row 1 Col 2|Row 2 Col 1 Row 2 Col 2|
这是不正确的。
是否有办法移除 `app-row-component` 的包装元素,以便我的网格样式仍然有效?或者,是否有另一种组织样式的方法,使我能够拥有一个每一行都是一个组件的网格?
英文:
Let's say I have a grid like so
<div style="display:grid; grid-template-columns auto auto;">
<div>Row 1 Col 1</div><div>Row 1 Col 2</div>
<div>Row 2 Col 1</div><div>Row 2 Col 2</div>
</div>
Which comes out looking like this:
|Row 1 Col 1|Row 1 Col 2|
|Row 2 Col 1|Row 2 Col 2|
Perfect, just what I want.
Now let's say I want to refactor my code so that each row is an angular component
<div style="display:grid; grid-template-columns auto auto;">
<app-row-component row=1></app-row-component>
<app-row-component row=2></app-row-component>
</div>
And app-row-component
looks something like this:
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
Now the styling of the grid no longer works, because angular wraps the content of app-row-component
in its own element, and the grid comes out looking like this
|Row 1 Col 1 Row 1 Col 2|Row 2 Col 1 Row 2 Col 2|
Which is not correct.
Is there a way I can remove the wrapping element of the app-row-component
so my grid styling still works? Alternatively, is there another way to organize my styling so that I have a grid where each row is a component?
答案1
得分: 1
just move the grid styling one level deeper:
<div style="display:grid; grid-template-columns: auto auto;">
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
</div>
If you want a higher level styling, then use some classes like this:
<div class="row">
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
</div>
<div class="table">
<app-row-2 [row]="1"></app-row-2>
<app-row-2 [row]="2"></app-row-2>
</div>
And a simple selector:
.table .row {
display: grid;
grid-template-columns: auto auto;
}
Stackblitz is updated.
I tested gre_gor's solution, for me it didn't work. Maybe I do something wrong.
I have updated the stackblitz with display: contents
, it's working now.
英文:
just move the grid styling one level deeper:
<div style="display:grid; grid-template-columns: auto auto;">
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
</div>
If you want a higher level styling, then use some classes like this:
<div class="row">
<div>Row {{row}} Col 1</div> <div>Row {{row}} Col 2</div>
</div>
<div class="table">
<app-row-2 [row]="1"></app-row-2>
<app-row-2 [row]="2"></app-row-2>
</div>
And a simple selector:
.table .row {
display: grid;
grid-template-columns: auto auto;
}
Stackblitz is updated.
I tested gre_gor's solution, for me it didn't work. Maybe I do something wrong.
I have updated the stackblitz with display: contents
, it's working now.
答案2
得分: 1
你需要将 display: contents;
应用到 app-row-component
。这将使CSS网格忽略该元素并定位其子元素。
在你的组件中添加 styles: [":host { display: contents; }"]
。
英文:
You need to apply display: contents;
to the app-row-component
.
This will make CSS grid ignore the element and position its children.
Add styles: [":host { display: contents; }"]
to your component.
答案3
得分: 0
建议但不强制使用选择器像app-row-component
。
您可以选择不同的选择器,例如[app-row-component]
:
<div style="display:grid; grid-template-columns auto auto;">
<div app-row-component row=1></div>
<div app-row-component row=2></div>
</div>
英文:
It is recommmended but not mandatory to use selectors like app-row-component
.
You can choose a different selector e.g. [app-row-component]
:
<div style="display:grid; grid-template-columns auto auto;">
<div app-row-component row=1></div>
<div app-row-component row=2></div>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论