I have a user component for 2 templates. First template for users table, second one for a user page. I select which template to use by role
attribute.
First example of using:
<table>
<tr user *ngFor="let user of users" [user]="user" role="UserTableItem"></tr>
</table>
In another one template I use my component like this:
<div user [user]="user" role="UserCard"></div>
So, my user
component template:
// user.template.html
<user-card [user]="user" *ngIf="role === 'UserCard'"></user-card>
<user-list-item [user]="user" *ngIf="role === 'UserListItem'"></user-list-item>
As we can see, here two components user-card
and user-list-item
. user-card
contains div
blocks, user-list-item
contains td
blocks. And table crashed because I have a <user-list-item>
block inside and my table looks like:
<table>
<tr>
<user-list-item>
<td></td>
<td></td>
</user-list-item>
</tr>
</table>
How can I solve my problem and get table like this?
<table>
<tr>
<td></td>
<td></td>
</tr>
</table>
UPD:
My user
component:
// user.component.ts
import { Component, Input, Inject, Attribute } from '@angular/core';
import { UserCard } from './userCard.component';
import { UserListItem } from './userListItem.component';
@Component({
selector: '[user]',
templateUrl: './user.template.html',
directives: [UserCard, UserListItem]
})
export class User {
@Input() user:any = null;
role:string;
constructor(
@Attribute('role') role) {
this.role = role;
}
}
userListItem.template.html
:
<td>
{{user.id}}
</td>
<td>
<img src="{{user.avatarUrl160}}" alt="User profile picture" width="160">
</td>
// ...
userCard.template.html
:
<div class="card card-block" *ngIf="user">
<h4 class="card-title">
User #{{user.id}}
<span class="tag tag-success" *ngIf="!user.isBanned">Active</span>
<span class="tag tag-danger" *ngIf="user.isBanned">Banned</span>
<span class="tag tag-danger" *ngIf="user.isDeleted">Deleted</span>
</h4>
<p>
<img width="340" src="{{user.avatarUrl160}}">
// ...
</div>
<div class="card card-block" *ngIf="user">
<span class="text-muted">Company: </span> {{user.company.name}}<br>
<span class="text-muted">Company logo: </span>
// ...
I find your question a bit confusing but I guess what you want is an attribute selector
@Component({
selector: '[user-list-item]',
...
})
export class UserListItem { ... }
and instead of
<user-card [user]="user" *ngIf="role === 'UserCard'"></user-card>
<user-list-item [user]="user" *ngIf="role === 'UserListItem'"></user-list-item>
use
<td user-card [user]="user" *ngIf="role === 'UserCard'"></td>
<td user-list-item [user]="user" *ngIf="role === 'UserListItem'"></td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With