【问题标题】:Create repeatable Angular component with two rows for tables为表格创建两行可重复的 Angular 组件
【发布时间】:2019-11-03 18:24:56
【问题描述】:

我有两个组件。第一个代表项目表,第二个代表一个项目。第一个重复第二个很多次。

列表组件 (app-list):

<table>
    <tr *ngFor="let item of items" [item]="item" app-item></tr>    
</table>

物品组件 (app-item):

<td>
    <img src="https://someimage.com/{{item.img}}.jpg">
</td>
<td>
    <h3>{{item.name}}</h3>
</td>
<td>
    {{item.description}}
</td>

为了让它工作,我必须为app-item 组件使用属性选择器:

@Component({
  selector: '[app-item]'
})

这很好用。


现在我想改进它并在每个app-item 中添加第二行。我的问题是 tr 标签位于 app-list 组件中,而不是 app-item 组件中。我想如果我将它移动到app-item 组件,我可以添加另一个tr 并且能够每一项显示两行。所以这就是我所做的。之后我使用ng-container 重复我的app-list 中的项目,以避免在我的两行周围添加包装标签:

<ng-container *ngFor="let item of items" [item]="item" app-item></ng-container>

此解决方案不起作用。我收到以下错误:

ERROR TypeError: el.setAttribute is not a function
    at EmulatedEncapsulationDomRenderer2.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.DefaultDomRenderer2.setAttribute (platform-browser.js:1089)
    at EmulatedEncapsulationDomRenderer2.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.EmulatedEncapsulationDomRenderer2.applyToHost (platform-browser.js:1157)
    at DomRendererFactory2.push../node_modules/@angular/platform-browser/fesm5/platform-browser.js.DomRendererFactory2.createRenderer (platform-browser.js:1015)

您能帮我解决此错误或建议其他实施吗?


编辑:解决方案

@Serhiy 建议的更好版本

表格:

<table>
  <app-item *ngFor="let item of items" [item]="item" remove-component-tag></app-item>
</table>

指令:

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[remove-component-tag]'
})
export class RemoveComponentTagDirective {
  constructor(private el: ElementRef) {

    let element = el.nativeElement;
    let children = el.nativeElement.childNodes;

    setTimeout(()=>{
      let reversedChildren = [];
      children.forEach(child => {
        reversedChildren.unshift(child);
      });
      reversedChildren.forEach(child => {
        element.parentNode.insertBefore(child, element.nextSibling);
      });
      element.remove(element);
    }, 0);

  }
}

由于某种原因超时是必要的,即使是 0 也可以。

【问题讨论】:

    标签: html angular components


    【解决方案1】:

    感谢您的解决方案。 我想为您的代码添加更正以避免使用 setTimeout 函数。 为指令实现OnInit 接口并将代码从构造函数移动到ngOnInit 方法将保持代码干净。

    import { Directive, OnInit, ElementRef } from '@angular/core';
    
    @Directive({
      selector: '[remove-component-tag]'
    })
    export class RemoveComponentTagDirective implements OnInit{
    
      constructor(private el: ElementRef) { }
    
      ngOnInit() {
        let element = this.el.nativeElement;
        let children = this.el.nativeElement.childNodes;
    
        let reversedChildren = [];
        children.forEach(child => {
          reversedChildren.unshift(child);
        });
        reversedChildren.forEach(child => {
          element.parentNode.insertBefore(child, element.nextSibling);
        });
        element.remove(element);
      }
    }
    

    看看Angular lifecycle hooks

    【讨论】:

      【解决方案2】:

      我看不到正确的“角度”方式,但您应该能够在渲染期间使用指令来清除您的 html。

      在这里的 cmets 中看到了这种方法:Angular2 : render a component without its wrapping tag

      我试过了,它对我有用:

      父组件:

      <table>
      
        <div *ngFor="let item of items">
          <app-item [item]="item" remove-wrapper></app-item>  
        </div>
      
      </table>
      

      子组件:

      <tr>
          <td>
            <img src="https://someimage.com/{{item.img}}.jpg">
          </td>
      
          <td>
            <h3>{{item.name}}</h3>
          </td>
      
          <td>
            {{item.description}}
          </td>      
      
      </tr>
      
      <tr>
          <td>
            <img src="https://someimage.com/{{item.img}}.jpg">
          </td>
      
          <td>
            <h3>{{item.name + ' 2'}}</h3>
          </td>
      
          <td>
            {{item.description + ' 2'}}
          </td>      
      
      </tr>
      

      指令:

      @Directive({
        selector: '[remove-wrapper]'
      })
      
      export class RemoveWrapperDirective {
      
        constructor(private el: ElementRef) {
      
          let parent = el.nativeElement.parentElement;
          let children = el.nativeElement.childNodes;
      
          setTimeout(()=>{
            parent.parentNode.insertBefore(children[1], parent.nextSibling);
            parent.parentNode.insertBefore(children[0], parent.nextSibling);
            parent.remove(parent);
          }, 10);
      
        }
      }
      

      没有超时,我就崩溃了。代码可以改进,但你可以从这里开始。

      【讨论】:

      • 这有点hacky,但它应该可以工作。我想知道是否有另一种“更合适”的方法来做到这一点
      【解决方案3】:

      在这里使用组件语法而不是指令语法会有帮助吗?

      代替:

      <ng-container *ngFor="let item of items" [item]="item" app-item></ng-container>
      

      试试:

      <ng-container *ngFor="let item of items">
          <app-item [item]="item"></app-item>
      </ng-container> 
      

      【讨论】:

      • 我已经试过了。 app-item 标记显示在 tr 标记周围的最终 HTML 中,这会破坏表格。我的目标是摆脱除 trs 之外的所有其他标签
      猜你喜欢
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 2016-12-24
      • 2012-12-31
      • 2018-09-24
      • 2017-11-14
      • 2016-01-13
      • 2021-08-02
      相关资源
      最近更新 更多