带有参数的Angular2 DynamicComponentLoader

GT

我正在尝试创建一个表组件,该组件将从列描述和原始数据构建一个表。

我能够毫无问题地创建基本功能,但是我也希望能够覆盖单个单元格的默认呈现,以便能够显示自定义内容(例如,显示链接)。

以与DataTables相似的方式,唯一的区别是,我希望能够为带有参数的自定义组件传递名称。

我最初的尝试是这样的,但问题是,如果我在列的显示函数中实例化该组件,则无法在页面上显示它。

@Component({
  selector: 'gt-table',
  template: `
  <table class="table table-striped table-hover">
    <thead>
      <tr>
        <th *ngFor="#column of columns">
          {{ column.title }}
        </th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="#row of data">
        <td *ngFor="#column of columns">
          {{ displayCell(row, column) }}
        </td>
      </tr>
    </tbody>
  </table>
  `
  })
  export class GTTableComponent implements {
    @Input() data:Array<any>;
    @Input() columns:Array<any>;

  displayCell(row, column) {
    if (column.display !== undefined) {
      return column.display(row[column.name], row);
    } else {
      return row[column.name];
    }
  }
}

在Internet上搜索之后,我发现要在视图中动态加载组件,您需要使用DynamicComponentLoader

经过很多头痛和搜索之后,即使它不是最漂亮的,我仍然能够找到与我在此响应中寻找的东西类似的东西

我唯一剩下的问题是如何传递参数?我找不到有关使用参数动态加载组件的单个参考。任何帮助或建议?

解决方案:

因此,解决方案是使用DynamicComponentLoaderloadIntoLocation功能。这样做loadAsRoot会比较理想,但是从2.0.0-beta.7它的状态来看,它有很多问题,并且您无法根据promise设置实例属性。

因此,我要做的是为单元格创建一个组件,在其中确定是否需要实例化默认显示组件或用户是否要处理它:

import {Component, Input, OnInit, ElementRef, DynamicComponentLoader, 
ComponentRef} from 'angular2/core';

import {GTTableDefaultDisplayComponent} from './gt-tabledefaultdisplay.component';
@Component({
    selector: '[gt-cell]',
    template: `<div #content></div>`,
    directives: []
})
export class GTTableCellComponent implements OnInit {
  @Input() row:any;
  @Input() column:any;

  constructor(private _loader: DynamicComponentLoader, private _elementRef: ElementRef) {
  }

  ngOnInit() {
    if (this.column.display !== undefined) {
      this.column.display(this._loader, this._elementRef, 'content',
      this.row[this.column.name],this.row);
    } else {
      this._loader.loadIntoLocation(GTTableDefaultDisplayComponent,
      this._elementRef, 'content').then((compRef:ComponentRef) => {
        compRef.instance['content'] = this.row[this.column.name];
      });
    }
  }
}

因此,使用属性加载动态组件的方法是将其设置为使用所返回的Promise compRef:ComponentRef这将是一个变量,您可以在其中访问新创建的组件的实例,compRef.instance并将其用作设置其变量的数组。

我的默认视图组件是如此简单:从“ angular2 / core”导入{Component,Input};

import {GTTableLinkComponent} from './gt-tablelink.component';

@Component({
    selector: 'default-display',
    template: `<div>{{content}}</div>`,
    directives: []
})
export class GTTableDefaultDisplayComponent {
  @Input() content:string;

  constructor() {
  }
}

我希望这也会对其他人有所帮助。

蒂埃里圣堂武士

您可以使用返回的承诺loadAsRoot(或loadNextToLocationloadIntoLocation)方法来访问它的新组件实例,并集合中的元素:

dcl.loadAsRoot(ChildComponent, '#child',
   injector).then((compRef:ComponentRef) => {

  // For example
  compRef.param1 = 'something';

  // In your case
  compRef.data = [
    { ... },
    { ... },
    { ... }
  ];
  compRef.columns = [
    'column1', 'column2'
  ];

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular2 DynamicComponentLoader

来自分类Dev

Angular2 beta中的DynamicComponentLoader:“元素上没有组件指令”

来自分类Dev

如何销毁使用angular2中的DynamicComponentLoader创建的所有组件?

来自分类Dev

Angular 2 DynamicComponentLoader的动态“模板变量名称”?

来自分类Dev

Angular2:循环dynamiccomponentloader将数组中的每个对象推到dom上?

来自分类Dev

Angular2:DynamicComponentLoader,其组件使用<ng-content> </ ng-content>

来自分类Dev

Angular2:DynamicComponentLoader,其组件使用<ng-content> </ ng-content>

来自分类Dev

DynamicComponentLoader is not recognizing element created in runtime

来自分类Dev

Angular2中带有可选参数的依赖注入

来自分类Dev

带有参数的Angular2辅助路线

来自分类Dev

带有附加 url 的 angular2 路由参数

来自分类Dev

DynamicComponentLoader无法识别在运行时创建的元素

来自分类Dev

类型“ DynamicComponentLoader”上不存在属性“ loadIntoLocation”

来自分类Dev

angular2:带有模板的服务?

来自分类Dev

带有Angular2的非SPA

来自分类Dev

带有 Angular2 图像的 Ngif

来自分类Dev

Angular2路由器可以激活,带有参数吗?

来自分类Dev

Angular2 http获取带有Observables和动态url参数的请求。如何?

来自分类Dev

Angular2路由配置(带有两个参数的路由)

来自分类Dev

Typescript中带有angular2的对象类型的数组

来自分类Dev

带有实时和WebSockets的Angular2 + Laravel

来自分类Dev

angular2 rxjs过滤带有输入文本的线程

来自分类Dev

Angular2中带有ngFor的ngControl

来自分类Dev

带有TypeScript的Angular2:将组件导入组件

来自分类Dev

带有Angular2和Typescript的Visual Studio 2013

来自分类Dev

带有Angular2的TypeScript中的Observable规范

来自分类Dev

带有Angular2的Gulp打字稿tsconfig

来自分类Dev

带有Material Design Lite的Angular2

来自分类Dev

Angular2:<li>带有* ngFor不在<ul>内

Related 相关文章

  1. 1

    Angular2 DynamicComponentLoader

  2. 2

    Angular2 beta中的DynamicComponentLoader:“元素上没有组件指令”

  3. 3

    如何销毁使用angular2中的DynamicComponentLoader创建的所有组件?

  4. 4

    Angular 2 DynamicComponentLoader的动态“模板变量名称”?

  5. 5

    Angular2:循环dynamiccomponentloader将数组中的每个对象推到dom上?

  6. 6

    Angular2:DynamicComponentLoader,其组件使用<ng-content> </ ng-content>

  7. 7

    Angular2:DynamicComponentLoader,其组件使用<ng-content> </ ng-content>

  8. 8

    DynamicComponentLoader is not recognizing element created in runtime

  9. 9

    Angular2中带有可选参数的依赖注入

  10. 10

    带有参数的Angular2辅助路线

  11. 11

    带有附加 url 的 angular2 路由参数

  12. 12

    DynamicComponentLoader无法识别在运行时创建的元素

  13. 13

    类型“ DynamicComponentLoader”上不存在属性“ loadIntoLocation”

  14. 14

    angular2:带有模板的服务?

  15. 15

    带有Angular2的非SPA

  16. 16

    带有 Angular2 图像的 Ngif

  17. 17

    Angular2路由器可以激活,带有参数吗?

  18. 18

    Angular2 http获取带有Observables和动态url参数的请求。如何?

  19. 19

    Angular2路由配置(带有两个参数的路由)

  20. 20

    Typescript中带有angular2的对象类型的数组

  21. 21

    带有实时和WebSockets的Angular2 + Laravel

  22. 22

    angular2 rxjs过滤带有输入文本的线程

  23. 23

    Angular2中带有ngFor的ngControl

  24. 24

    带有TypeScript的Angular2:将组件导入组件

  25. 25

    带有Angular2和Typescript的Visual Studio 2013

  26. 26

    带有Angular2的TypeScript中的Observable规范

  27. 27

    带有Angular2的Gulp打字稿tsconfig

  28. 28

    带有Material Design Lite的Angular2

  29. 29

    Angular2:<li>带有* ngFor不在<ul>内

热门标签

归档