首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular在ng-container中使用父作用域

Angular在ng-container中使用父作用域
EN

Stack Overflow用户
提问于 2020-03-03 05:03:24
回答 1查看 222关注 0票数 0

我有一个带有一些配置的网格组件。

我想在使用组件时定义a和tbody。这是我的组件

代码语言:javascript
复制
@Component({
selector: 'tcs-grid',
templateUrl: './tcs-grid.component.html',
styleUrls: ['./tcs-grid.component.scss']
})

export class TcsGridComponent implements OnInit {
data : any;
//some configs
}

这是Html模板

代码语言:javascript
复制
  <table datatable class="table tablesorter" [dtOptions]="dtOptions">
      <ng-content>

      </ng-content>
  </table>

现在我想这样使用它

用法:

代码语言:javascript
复制
<tcs-grid>
<thead >
    <tr>
      <th width="50px" >Row</th>
      <th>ID</th>
      <th>First name</th>
      <th>Last name</th>
    </tr>
  </thead>
  <tbody *ngIf="data?.length != 0">
    <tr *ngFor="let person of data;index as i">
        <td>{{ i + startPoint }}</td>
        <td>{{ person.id }}</td>
        <td>{{ person.firstName }}</td>
        <td>{{ person.lastName }}</td>
    </tr>
</tbody>
</tcs-grid>

我知道使用事件发射器可以输出数据。我想知道是否有更好的方法来访问网格组件(父组件)中的数据。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-04 04:32:31

我使用ng-template找到了解决方案,你可以创建部分模板!

为了绑定父作用域变量,不要忘记添加上下文

模板:

代码语言:javascript
复制
<table datatable class="table tablesorter" [dtOptions]="dtOptions">
   <ng-container  *ngTemplateOutlet="layoutTemplate; context: {persons: data}"  >

   </ng-container>
</table>

组件:

代码语言:javascript
复制
export class TcsGridComponent implements OnInit {
@ContentChild(TemplateRef,null)
@Input() layoutTemplate: TemplateRef<any>;

data : any;

ngAfterContentInit()
{
//your code 
}
}

用法:

代码语言:javascript
复制
 <tcs-grid>
 <ng-template let-persons="persons">
    <thead >
    <tr>
      <th width="50px" >Row</th>
      <th>ID</th>
      <th>First name</th>
      <th>Last name</th>
    </tr>
  </thead>
   <tbody *ngIf="persons?.length != 0">
    <tr *ngFor="let person of persons;index as i">
        <td>{{ i + startPoint }}</td>
        <td>{{ person.id }}</td>
        <td>{{ person.firstName }}</td>
        <td>{{ person.lastName }}</td>
    </tr>
   </tbody>
  </ng-template >
  </tcs-grid>

您可以通过命名let- You access来访问父作用域中的变量。

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

https://stackoverflow.com/questions/60496601

复制
相关文章

相似问题

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