首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问角8/9/10/11可重用构件中的子模板

如何访问角8/9/10/11可重用构件中的子模板
EN

Stack Overflow用户
提问于 2020-12-18 09:33:36
回答 1查看 648关注 0票数 3

我已经创建了通用组件,它将以表格格式呈现数据。

data-table.component.ts (这是可重用的组件,任何子组件都可以使用)

代码语言:javascript
复制
    @Component({
      selector: 'data-table',
      templateUrl: './data-table.component.html',
      styleUrls: ['./data-table.component.css']
    })
    export class DataTableComponent implements OnInit {
    constructor() { }
    
      ngOnInit(): void {
         //wanted to access child here(or inside any method in this class) to set the width of each <td> from child
      //wanted to access #rowTemplate children from child for all 3 <td> 
      }
setColumnWidth(width: string = "") {
    if (width != "") {
      return 'width:' + width;
    }
  }
    }

data-table.component.html

代码语言:javascript
复制
<table style="width:100%;">
      <thead>
        <tr>
          <th *ngFor="let c of columns" style="{{setColumnWidth(c.width)}}">
            {{c.fieldTitle}}
          </th>
        </tr>
      </thead>
       <tbody>
        <ng-container *ngTemplateOutlet="rowTemplate"></ng-container>
      </tbody>
    </table>

resource-updates.component.ts (这是子组件)

代码语言:javascript
复制
    @Component({
      selector: 'resource-updates',
      templateUrl: './resource-updates.component.html',
      styleUrls: ['./resource-updates.component.css']
    })
    export class ResourceUpdatesComponent implements OnInit {
      columns: Column[] = [];
      rows: any[] = [];
     
      constructor() { }
    
      ngOnInit(): void {
        //Preparing list for columns
        this.columns.push(new Column({ fieldName: 'Id', fieldTitle: 'Id', isSortable: false, width: '5%' }));
        this.columns.push(new Column({ fieldName: 'Title', fieldTitle: 'Title', width: '60%' }));
        this.columns.push(new Column({ fieldName: 'Type', fieldTitle: 'Type', width: '35%' }));
      
       //Preparing list for rows
       this.rows = [
{"id":5,"title":"Air Gas Guideline","type":"PPT"},
{"id":6,"title":"Air Pollution User Reference","type":"Website"},
{"id":18,"title":"Avian Influenza (H7N9) User Reference","type":"Website"},
{"id":12,"title":"Avian Influenza (H7N9) for high risk","type":"PPT"},
{"id":11,"title":"Avian Influenza (H7N9) for normal","type":"PPT"}
];
      }
    }

resource-updates.component.html (希望在columns.width属性的父级(公共组件)中设置宽度为3)

代码语言:javascript
复制
  <ng-template #rowTemplate>
    <tr *ngFor="let r of rows">
      <td>{{r.id}}</td>
      <td>{{r.title}}</td>
      <td>{{r.type}}</td>
    </tr>
  </ng-template>
  <data-table [columns]="columns" [rowTemplate]="rowTemplate"></data-table>

任何机构可以帮助访问#rowTemplate子元素,以便从columns.width和.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-18 09:47:56

如果我没有理解错,您想从父组件访问子组件的ng模板吗?如果是的话,这就是我以前在创建数据表控件时使用的情况。

  1. 您可以创建一个包含TemplateRef的指令类:
代码语言:javascript
复制
import { Directive, ContentChild, TemplateRef, Input } from "@angular/core";
@Directive({ selector: "np-column" })
export class NpColumnDirective {
      @Input() name: string;
      @ContentChild(TemplateRef) cellTemplate: TemplateRef<any>;
      constructor() { }
}
  1. 然后,在您的父级组合中,使用以下方法访问它:

@ContentChildren(NpColumnDirective) columnComponents: QueryList<NpColumnDirective>;

下面是我用来创建数据表的全部代码,仅供参考:数据表演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65354605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档