Accessing the view of a child component in Angular from its parent component

In my parent component's template, I have inserted a table with each row being a child component that loops through *ngFor.

Parent template :

<table style="width:100%">
    <tr>
        <th>Id</th>
        <th>First Name</th> 
        <th>Last Name</th>
    </tr>
    <app-liste-client-row *ngFor="let cli of clients" [client]="cli" >
    </app-liste-client-row>
</table>

Template for the child component:

<tr>
    <td>{{client.id}}</td>
    <td>{{client.firstName}}</td>
    <td>{{client.lastName}}</td>
</tr>

The advantage is that all the rows of the table are displayed.

The drawback is that the data in the rows do not align perfectly with the columns of the table. I tried using CSS (using IDs with margin and padding), but it doesn't seem like an ideal solution as I had to adjust the values manually. Also, I don't want to directly code the loop into my parent component either.

What is the best and most efficient way to create a clean table structure using parent and child components? Am I missing something in terms of utilizing CSS effectively?

Answer №1

Avoid using the following code snippet as a child component:

<tr>
    <td>{{client.id}}</td>
    <td>{{client.firstName}}</td>
    <td>{{client.lastName}}</td>
</tr>

Since tr elements are typically used within tables, it's recommended to create a separate component if you plan on reusing this structure in multiple parts of your application. If you anticipate using an entire table multiple times, consider keeping it within the parent component.

If you need to reuse data, consider creating a service that can provide the necessary information.

However, if you still decide to proceed with this approach, you may explore utilizing templates like the ones mentioned here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The value of a Mat dialog within a mat table will remain consistent when an addrow action is performed

After attempting to open my table, here is what I discovered: https://i.sstatic.net/F3zCZ.png When opening the dialog box, it displays a list on my table. https://i.sstatic.net/SyJJk.png However, upon clicking "add row," it duplicates the value for me. ...

Enhancing User Experience with React Hover Effects

I'm currently in the process of developing a React component library, and I'm looking to enhance the components with some stylish flair. Everything seems to be working smoothly, but I'm interested in changing the background color when a user ...

Adjust focus dynamically on pressing enter in an HTML form

I am currently working on adjusting the functionality of my HTML input form to change focus upon pressing the enter key. I need to trigger the saveValue() function from the input field when the enter key is pressed and then move the focus to the next input ...

Encountering a problem while implementing ngFor in Angular 4

Issue with ngFor Loop in Angular 4 Does anyone have insight into why this error is appearing in the console despite data being displayed? The data is showing, but an error is still being thrown empresas = <Empresa> {}; constructor(private servi ...

How should one go about properly implementing the ng2 sequence to refresh a component display based on a dropdown selection?

How can I properly refresh a component's display in ng2 based on dropdown selection? Within my application, there is a users.component and a users.service responsible for searching for users based on a provided request object. Within the users compon ...

Async pipe in Angular does not work with my custom observables

I've been trying to implement the async pipe in my template, but I'm encountering difficulties retrieving data from my API. To store and retrieve the data, I have utilized a behavior subject to create an observable. However, when I attempt to dis ...

Error: Trying to access the 'cvs' property of an undefined object in a class during initialization

Recently, I created a class called "genGameObject" which is intended to generate a Game Object and draw content inside of it. Initially, I suspected it was just a minor typing error, but after thorough inspection, I couldn't find any (If you happen t ...

Utilizing pure CSS to target the first and last classes

I want to use pure CSS to select only the first and last classes throughout the entire document. In my scenario, the structure looks like this: <div class="Container"> <div> <div class="Parent"></div> <pre> ...

The ngx-daterangepicker-material is throwing an error: "A declaration of an accessor is not allowed in an

I'm experiencing an error in my package.json file and could use some assistance. The specific error message I keep encountering is: ERROR in node_modules/ngx-daterangepicker-material/daterangepicker.component.d.ts(59,9): error TS1086: An accessor can ...

Guide to updating div card content when hovering in Vuejs

new Vue({ el: '#mouse', data: { message: 'Hover Me!' }, methods: { mouseover: function(){ this.message = 'Good!' }, mouseleave: function(){ this.message = 'Hover Me!' } ...

Setting view encapsulation globally in Angular2 allows you to define a consistent

I have made the decision to globally set ViewEncapsulation.None for all the components in my project. Instead of having to manually set it in each component's decorator, how can I apply this setting project-wide? Note: For reference, my dependencies ...

Issue with activation of onClick event in case/switch statement

Currently working on a JavaScript project to recreate the Fallout terminal game, with one of the main aspects being comparing words selected by the user to those chosen by the computer. The concept of this hacking game is reminiscent of the board game Mas ...

Why is it that my JQuery sliders appear perfectly fine when viewed locally, but fail to show up when accessed on

I'm facing an issue with two JQuery sliders on my page. The local version works fine, but when I upload it to my web host, only one slider functions correctly. I need both of them to work as intended. Any insights on how to resolve this problem? See ...

Mastering the art of incorporating objects into templates using *ngFor Angular

Whenever I do my *ngFor loop in my HTML template, the data is displaying as [Object Object]. Below is my view with the enumerated data in the developer console: https://i.stack.imgur.com/KXmiI.png This is the snippet of my HTML code: https://i.stack.im ...

Is there a possibility that typescript decorators' features will be polyfilled in browsers lacking ES5 support?

According to the typescript documentation, a warning is issued: WARNING  If your script target is lower than ES5, the Property Descriptor will be undefined. If the method decorator returns a value, it will act as the Property Descriptor for the method. ...

Issue with Angular: Unable to bind to 'ngForFrom' as it is not recognized as a valid property of 'a'

I seem to be encountering a recurring error that I can't seem to resolve, despite trying various solutions. Below is the content of my appModule.ts file: import { CommonModule } from '@angular/common'; import { NgModule } from '@angula ...

Cease the RxJS HTTP Polling upon receiving a successful HTTP response

In my code, I have implemented a polling mechanism to mimic HTTP Requests. The goal is to stop sending requests if the server responds with data.length > 0. timeout:Observable<number> = timer(10000); startPollingStackblitz(arnId: string) { ...

Is there a way I can position my privacy alert so that it appears in front of all DIVs, RevSlider,

Have you had a chance to visit my website at ? I recently added a privacy alert in compliance with the new EU Regulation. However, I'm facing an issue where the alert is overlapping with some of my website components such as other DIVs and Revolution ...

Ensure that the div is set to start on a new line without causing any sudden jumps to the

I have 3 divs that I want to display side by side. If the text inside each div gets too long, I would like it to break to a new line and have the next div stay beside it. <div style="float: left;">This is some text</div> <div style="flo ...

What is the best way to choose a specific section of a string using JQuery?

When utilizing the .html() function to retrieve html code, I encounter a need to extract a specific segment of the string. var newImgAdr = $(this).html(); The variable newImgAdr currently consists of: <img class="thumbnail-holder image-responsive cen ...