Embedding an Angular Directive in <tr>

老子叫甜甜 提交于 2019-12-23 23:12:07

问题


I'm trying to get an angular directive to repeat inside a <tr>. Without the directive, the code is

<tr *ngFor='let cluster of clusters'>
    <td style='display: flex; flex-direction: column; width: 40%; min-width: 300px;'>
        <span>{{ cluster.Name }}</span>
        <span>{{ cluster.StatusMessage }}</span>
    </td>
    <td style='width: 100%;'>
        ...
    </td>
</tr>

And looks like

When I refactor the directive, I lose the <tr> formatting. The refactored code is

<tr *ngFor='let cluster of clusters'>
    <cluster-row [name]='cluster.Name' [statusMessage]='cluster.StatusMessage'></cluster-row>
</tr>

The cluster-row component code is:

<td style='display: flex; flex-direction: column; width: 40%; min-width: 300px;'>
    <span>{{ name }}</span>
    <span>{{ statusMessage }}</span>
</td>
<td style='width: 100%;'>
    ...
</td>

It works, but the <tr> formatting goes away.

The developer tools show a <tr> with a <cluster-row> nested in it, and two <td>s nested in that, as I would expect.


回答1:


<tr> can't contain a <cluster-row> element.

You can use an attribute selector instead like

<tr *ngFor='let cluster of clusters'>
    <td cluster-row [name]='cluster.Name' [statusMessage]='cluster.StatusMessage'></cluster-row>
</tr>

with

@Component({
//@Directive({
  selector: '[cluster-row]'

The component or directive then is the <td>, therefore you can't repeat it inside the components template

style='display: flex; flex-direction: column; width: 40%; min-width: 300px;'

can just be added to

@Component({ 
  selector: '[cluster-row]',
  styles: [...],


来源:https://stackoverflow.com/questions/43124637/embedding-an-angular-directive-in-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!