Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular2不适合我的用例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:我发现[ngTemplateOutlet]似乎更适合.讨论在 Can’t get ngTemplateOutlet to work继续进行

原始问题:

我已经构建了一个表组件,我将在多个不同的视图中使用它来呈现不同的数据.该数据及其表示应由表组件的使用者控制.

我尝试使用< ng-content>传递一个“渲染器”,它知道如何检索该数据以及如何呈现它.您可能理解,这是非法使用.

以下是我尝试使用< ng-content>的简化方法.

这是表组件模板:

<table>
  <thead>
  <tr>
    <th *ngFor="let column in columns">{{column.heading}}</th>
  <tr>
  </thead>
  <tbody>
  <tr *ngFor="let item of items>
    <td *ngFor="let column of columns>
      <ng-content SELEct="{{column.templatE}}"></ng-content>
    </td>
  </tr>
  </tbody> 
</table>

以下是表组件的用法

<my-table-component [items]="cars" [columns]="carcolumns">
  <div class="model">{{item.model}}</div>
  <div class="color">{{item.color}}</div>
  <div class="gearBox>{{item.gearBox}}</div>
</my-table-component>

以下是示例数据

cars = [
  { model: "volvo",color: "blue",gearBox: "manual" },{ model: "volvo",color: "yellow",{ model: "ford",gearBox: "automatic" },{ model: "mercedes",color: "silver",gearBox: "automatic" }
];
carcolumns = [
  { heading: "Model",template: ".model" },{ heading: "Color",template: ".color" },{ heading: "Gear Box",template: ".gearBox" }
];

通过这个我希望你明白我想做什么.显而易见的问题似乎是< ng-content>在“循环”中不起作用,它不接受动态“选择”.此外,item当然不能选择表组件使用者.

很可能还有其他“不要”.

因为我是棱角分明2的新手,所以我希望我误会了某些事情,或者说我使用了错误的工具.有任何想法吗?

解决方法

[ngTemplateOutlet]和[ngOutletContext]更适合这个用例.以下是相同用例的工作示例:
Can’t get ngTemplateOutlet to work

大佬总结

以上是大佬教程为你收集整理的Angular2不适合我的用例全部内容,希望文章能够帮你解决Angular2不适合我的用例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。