将Angular组件的内容直接嵌入,无需包装器

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

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

&lt;div style=&quot;display:grid; grid-template-columns auto auto;&quot;&gt;
    &lt;div&gt;Row 1 Col 1&lt;/div&gt;&lt;div&gt;Row 1 Col 2&lt;/div&gt;
    &lt;div&gt;Row 2 Col 1&lt;/div&gt;&lt;div&gt;Row 2 Col 2&lt;/div&gt;
&lt;/div&gt;

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

&lt;div style=&quot;display:grid; grid-template-columns auto auto;&quot;&gt;
    &lt;app-row-component row=1&gt;&lt;/app-row-component&gt;
    &lt;app-row-component row=2&gt;&lt;/app-row-component&gt;
&lt;/div&gt;

And app-row-component looks something like this:

&lt;div&gt;Row {{row}} Col 1&lt;/div&gt; &lt;div&gt;Row {{row}} Col 2&lt;/div&gt;

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:

&lt;div style=&quot;display:grid; grid-template-columns: auto auto;&quot;&gt;
      &lt;div&gt;Row {{row}} Col 1&lt;/div&gt; &lt;div&gt;Row {{row}} Col 2&lt;/div&gt;
    &lt;/div&gt;

Sample stackblitz

If you want a higher level styling, then use some classes like this:

&lt;div class=&quot;row&quot;&gt;
      &lt;div&gt;Row {{row}} Col 1&lt;/div&gt; &lt;div&gt;Row {{row}} Col 2&lt;/div&gt;
    &lt;/div&gt;

&lt;div class=&quot;table&quot;&gt;
      &lt;app-row-2 [row]=&quot;1&quot;&gt;&lt;/app-row-2&gt;
      &lt;app-row-2 [row]=&quot;2&quot;&gt;&lt;/app-row-2&gt;
    &lt;/div&gt;

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:

&lt;div style=&quot;display:grid; grid-template-columns: auto auto;&quot;&gt;
      &lt;div&gt;Row {{row}} Col 1&lt;/div&gt; &lt;div&gt;Row {{row}} Col 2&lt;/div&gt;
    &lt;/div&gt;

Sample stackblitz

If you want a higher level styling, then use some classes like this:

&lt;div class=&quot;row&quot;&gt;
      &lt;div&gt;Row {{row}} Col 1&lt;/div&gt; &lt;div&gt;Row {{row}} Col 2&lt;/div&gt;
    &lt;/div&gt;

&lt;div class=&quot;table&quot;&gt;
      &lt;app-row-2 [row]=&quot;1&quot;&gt;&lt;/app-row-2&gt;
      &lt;app-row-2 [row]=&quot;2&quot;&gt;&lt;/app-row-2&gt;
    &lt;/div&gt;

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: [&quot;:host { display: contents; }&quot;]

StackBlitz示例

英文:

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: [&quot;:host { display: contents; }&quot;] to your component.

StackBlitz example

答案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]:

&lt;div style=&quot;display:grid; grid-template-columns auto auto;&quot;&gt;
    &lt;div app-row-component row=1&gt;&lt;/div&gt;
    &lt;div app-row-component row=2&gt;&lt;/div&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年3月7日 01:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653824.html
匿名

发表评论

匿名网友

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

确定