0

users-component.html

<table class="table table-hover">
    <thead class="text table-head-font">
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>         
        </tr>
    </thead>
    <tbody>
        <tr (dblclick)="selectUser()">
            <td>{{id}}</td>
            <td>{{firstName}}</td>
            <td>{{lastName}}</td>            
        </tr>
    </tbody>
</table>

app.component.html

This will render thead for every record inside table, which I want to avoid.

I tried to refactor this to put table tr items inside users-component.html and table header to leave outside in app.component like app.component.html

<table class="table table-hover">
    <thead class="text table-head-font">
        <tr>
            <th>ID</th>
            <th>First Name</th>
            <th>Last Name</th>         
        </tr>
    </thead>
    <tbody>
        <users *ngFor="let user of users" (openUser)="getUser($event)" [id]="user.Id" [firstName]="this.firstName" [lastName]="this.lastName"></users>
    </tbody>
</table>

users-component.html

<tr (dblclick)="selectUser()">
    <td>{{id}}</td>
    <td>{{firstName}}</td>
    <td>{{lastName}}</td>            
</tr>

But this renders everything under first td, rest of td and tr are empty.

Update

users-component.ts

@Component({
    selector: 'users',
    templateUrl: './users-component.html'
})
user1765862
  • 13,635
  • 28
  • 115
  • 220

2 Answers2

0

In users-component.ts add a tr selector:

@Component({
    selector: 'tr',
    ...
})

Then in app.component.html use the users-component like this:

<tr users></tr>

dimlucas
  • 5,040
  • 7
  • 37
  • 54
0

Below is a way that can help you achieve this. You can pass you user list as an Input to the user component and then there itself display the users. Names may vary according to your choice.

Below is the code for reference.

Here is a live Demo of the below code app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  userList = [
    {
      id:"1",
      name:"user1",
      email:"shadfh@gfh.com"
    },
     {
      id:"2",
      name:"user2",
      email:"shadfh@gfh.com"
    },
     {
      id:"3",
      name:"user3",
      email:"shadfh@gfh.com"
    }
  ]
}

app.component.html

<users [users]="userList"></users>

user.component.html

<table class="table table-hover">
    <thead class="text table-head-font">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>         
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let user of users" (dblclick)="selectUser()">
            <td>{{user.id}}</td>
            <td>{{user.name}}</td>
            <td>{{user.email}}</td>            
        </tr>
    </tbody>
</table>

user.component.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'users',
  templateUrl: './user.component.html',
  styleUrls: [  ]
})
export class UserComponent  {
  @Input() users: any;
}

Hope this helps :)

Manish
  • 4,692
  • 3
  • 29
  • 41