如何移除Angular为组件添加的包装元素?

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

How do i remove the wrapper element added by Angular to a component?

问题

I can see that you want to remove the wrapper element Angular adds to app-scroll-child components. To achieve this, you can use Angular's <ng-container> as a wrapperless container. Here's how you can modify your code:

  1. import { ChangeDetectionStrategy, Component } from '@angular/core';
  2. @Component({
  3. selector: 'app-scroll-child',
  4. template: `
  5. <ng-container>
  6. <div class="snap-start shrink-0 grid w-full h-screen place-items-center text-8xl">
  7. <ng-content></ng-content>
  8. </div>
  9. </ng-container>
  10. `,
  11. changeDetection: ChangeDetectionStrategy.OnPush,
  12. standalone: true,
  13. })
  14. export class ScrollChildComponent {}

By wrapping the contents in <ng-container>, you can use <app-scroll-child> without the additional wrapper element:

  1. <app-scroll-container>
  2. <app-scroll-child>
  3. <div class="snap-start shrink-0 bg-amber-200 grid w-full h-screen place-items-center text-8xl">1</div>
  4. </app-scroll-child>
  5. <app-scroll-child>
  6. <div class="snap-start shrink-0 bg-blue-200 grid w-screen h-screen place-items-center text-8xl">2</div>
  7. </app-scroll-child>
  8. <app-scroll-child>
  9. <div class="snap-start shrink-0 bg-orange-200 grid w-screen h-screen place-items-center text-8xl">3</div>
  10. </app-scroll-child>
  11. <app-scroll-child>
  12. <div class="snap-start shrink-0 bg-green-200 grid w-screen h-screen place-items-center text-8xl">4</div>
  13. </app-scroll-child>
  14. </app-scroll-container>

This modification should allow you to use <app-scroll-child> without the extra wrapper element.

英文:

I am making an Angular app + Tailwind that is one large horizontal page that allows the user to scroll from left to right using scroll-snap-type. When i try to cleanup my code and create reusable components the rendered html is different than when i have all my HTML in one component.

I have created a ScrollContainerComponent:

  1. import { ChangeDetectionStrategy, Component, ElementRef, ViewChild } from &#39;@angular/core&#39;;
  2. import { CommonModule } from &#39;@angular/common&#39;;
  3. @Component({
  4. selector: &#39;app-scroll-container&#39;,
  5. standalone: true,
  6. template: `
  7. &lt;div
  8. class=&quot;flex snap-x snap-proximity h-screen w-full mx:auto overflow-x-scroll overflow-y-hidden &quot;
  9. #scrollContainer
  10. &gt;
  11. &lt;ng-content&gt;&lt;/ng-content&gt;
  12. &lt;/div&gt;
  13. `,
  14. changeDetection: ChangeDetectionStrategy.OnPush,
  15. })
  16. export class ScrollContainerComponent {
  17. @ViewChild(&#39;scrollContainer&#39;)
  18. scrollContainer!: ElementRef;
  19. ngAfterViewInit() {
  20. this.scrollContainer.nativeElement.addEventListener(&#39;keydown&#39;, this.handleKeydown.bind(this));
  21. }
  22. handleKeydown(event: KeyboardEvent) {
  23. switch (event.key) {
  24. case &#39;ArrowLeft&#39;:
  25. this.scrollLeft();
  26. break;
  27. case &#39;ArrowRight&#39;:
  28. this.scrollRight();
  29. break;
  30. }
  31. }
  32. scrollLeft() {
  33. this.scrollContainer.nativeElement.scrollLeft -= 100;
  34. }
  35. scrollRight() {
  36. this.scrollContainer.nativeElement.scrollLeft += 100;
  37. }
  38. }

And a child:

  1. import { ChangeDetectionStrategy, Component } from &#39;@angular/core&#39;;
  2. @Component({
  3. selector: &#39;app-scroll-child&#39;,
  4. template: `&lt;div class=&quot;snap-start shrink-0 grid w-full h-screen place-items-center text-8xl&quot;&gt;
  5. &lt;ng-content&gt;&lt;/ng-content&gt;
  6. &lt;/div&gt;`,
  7. changeDetection: ChangeDetectionStrategy.OnPush,
  8. standalone: true,
  9. })
  10. export class ScrollChildComponent {}

However the it only works when i use it like this:

  1. &lt;app-scroll-container&gt;
  2. &lt;div class=&quot;snap-start shrink-0 bg-amber-200 grid w-full h-screen place-items-center text-8xl&quot;&gt;1&lt;/div&gt;
  3. &lt;div class=&quot;snap-start shrink-0 bg-blue-200 grid w-screen h-screen place-items-center text-8xl&quot;&gt;2&lt;/div&gt;
  4. &lt;div class=&quot;snap-start shrink-0 bg-orange-200 grid w-screen h-screen place-items-center text-8xl&quot;&gt;3&lt;/div&gt;
  5. &lt;div class=&quot;snap-start shrink-0 bg-green-200 grid w-screen h-screen place-items-center text-8xl&quot;&gt;4&lt;/div&gt;
  6. &lt;/app-scroll-container&gt;

Instead of using the &lt;app-scroll-child&gt;1&lt;/app-scroll-child&gt;.

Please help me in removing the wrapper element angular adds to app-scroll-child

答案1

得分: 1

如果您的选择器挡住了视线,请查看 [ ]

选择器:'[app-scroll-child]'

您可以将其应用于任何 HTML 标记,例如 div

  1. <div app-scroll-child
  2. class="snap-start shrink-0 grid w-full h-screen place-items-center text-8xl">
  3. <div class="snap-start shrink-0 bg-amber-200..."></div>
  4. ...
  5. </div>
英文:

If your selector it's in the way, see the [ ]

  1. selector: &#39;[app-scroll-child]&#39;

You can applied to any html tag, e.g. a div

  1. &lt;div app-scroll-child
  2. class=&quot;snap-start shrink-0 grid w-full h-screen place-items-center text-8xl&quot;&gt;
  3. &lt;div class=&quot;snap-start shrink-0 bg-amber-200...&lt;/div&gt;
  4. ...
  5. &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年8月10日 22:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876725.html
匿名

发表评论

匿名网友

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

确定