Utilizing the p tag to incorporate dynamic data in a single line

I am currently working with two JSON files named contacts and workers. Using the workerId present in the contacts JSON, I have implemented a loop to display workers associated with each contact. The current display looks like this: https://i.sstatic.net/ARF8k.png

However, I am facing an issue where I am trying to showcase the workers objects in a single line as shown below:

https://i.sstatic.net/RJAsl.png

Even after applying CSS styling as display:inline;, the desired inline display is not achieved:

Refer to the component code snippet provided below:

HTML

<h4>Contacts</h4>
<div class="cust-detail" *ngFor="let contact of contacts">
    <p><span><b>Name:</b></span> {{contact.name }}</p>
  <span><b>Assigned Workers</b></span>
  <div *ngFor="let workerId of contact.workers">
     <p class="workers">
          {{getWorkerById(workerId)}}
      </p>
  </div> 
    <hr>
</div>

TS

import { Component } from '@angular/core';
import { ContactService } from './contacts.service';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  contacts;
  workers;

  constructor(private myService: ContactService) {}

  ngOnInit() {
    this.myService.getContacts()
      .subscribe(res => this.contacts = res);
       this.myService.getWorkers()
      .subscribe(res => this.workers = res);
  }

    getWorkerById(workerId) {
      if (this.workers && this.workers.length > 0) {
        for(let w of this.workers) {
          if(w.id === workerId) 
            return w.name;
        }
      }
   }

}

CSS

.workers{
  display:inline;
}

DEMO

Answer №1

Here is a suggestion for your upcoming comment

<h4>Contact Information</h4>
<div class="customer-info" *ngFor="let contact of contacts; let i = index">
    <tr>
        <td>Name</td>
        <td>{{contact.name }}</td>
    </tr>
    <tr>
        <td>Assigned Workers</td>
        <td>
      <div [ngClass]="{'all-workers' : showmore[i]==false }">
        <div *ngFor="let workerId of contact.workers;let j=index">
         <p class="workers" *ngIf="(showmore[i]==false &&j<=1)||showmore[i]==true">
           {{getWorkerById(workerId)}},
           </p>
      </div>
      </div>
       <button *ngIf="contact.workers.length>2" (click)="showmore[i]=!showmore[i]">{{(showmore[i])?"Show less":"Show more"}}</button> 
        </td>
    </tr>
    <hr>
</div>

Answer №2

Here is a sample of HTML code for you to try:

<h4>Contacts</h4>
<div class="cust-detail" *ngFor="let contact of contacts">
    <p><span><b>Name:</b></span> {{contact.name }}</p>
  <p><b>Assigned Workers</b></p>
  <span *ngFor="let workerId of contact.workers">
     <p class="workers">
          {{getWorkerById(workerId)}} &nbsp;
      </p>
  </span> 
    <hr>
</div>

Answer №3

To make the necessary updates, you should modify both the HTML and CSS as shown below:

  <h4>Contacts</h4>
    <div class="cust-detail" *ngFor="let contact of contacts">
      <p><span><b>Name:</b></span> {{contact.name }}</p>
      <span><b>Assigned Workers</b></span>
      <div class="all-workers">
        <div *ngFor="let workerId of contact.workers">
          <p class="workers">
            {{getWorkerById(workerId)}} &nbsp;
          </p>
        </div>
      </div>
      <hr>
    </div>

Additionally, update the css with the following:

.all-workers{
  display: flex;
}

View the Demo for more information.

Answer №4

Give this a try:

`

<p *ngFor="let workerId of contact.workers">
      {{getWorkerById(workerId)}}
  </p>

`

Simply use *ngFor on the p tag.

https://i.sstatic.net/mGBJj.png

This is what your view will look like after implementing *ngFor on the p tag.

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

Coloring the Text on Buttons

After nearly 24 hours of practicing, I still can't seem to wrap my head around it. Can someone assist me in changing the button text color to white, while keeping everything else the same? //NAVIGATION //======================== .nav ul display: ...

I Am unable to locate the '...' after applying the text-ellipsis style to a div

https://i.stack.imgur.com/Tsmf5.png The ellipsis '...' is not showing up even after I have applied text-ellipsis, overflow hidden, and nowrap to this div. Take a look at my code: import Image from "next/future/image"; import Link from ...

Surprising Logging Quirks in Angular

I've encountered a perplexing issue in my Angular app where an array is logged before adding an element to it, yet the log shows the updated array with the added element. This array is then utilized in an HTML file with ngFor. component.ts file inter ...

What is the process for retrieving the parent component from projected content?

One interesting aspect to explore is the varying behaviors of input based on its 'parent' element. The structure I am referring to is as follows: In my first example, the input is nested within the app-chip-list component. APP COMPONENT HTML & ...

Having difficulty sending a message from an AngularJS application to an Angular 10 application through an iframe

I have two applications running locally - one is an AngularJS application and the other is an Angular 10 application. I am trying to access a page from the Angular 10 application using an iframe and send a message to it from the AngularJS application. How ...

Angular 2 module that is loaded lazily - service does not follow singleton pattern

After successfully implementing lazy loading modules into my application, I have ensured that the app.module.ts is properly configured. @NgModule({ declarations: [ AppComponent, HeaderComponent, HomeComponent ], imports: [ BrowserMod ...

The method to permit a single special character to appear multiple times in a regular expression

I am currently working on developing a REGEX pattern that specifically allows alphanumeric characters along with one special character that can be repeated multiple times. The permitted special characters include ()-_,.$. For instance: abc_def is conside ...

How to Add a Responsive Inset Transparent Overlay to Images without Using JavaScript?

My goal is to create a unique overlay effect on images of different sizes within a Pinterest-style fluid grid layout. The outer border width and inset 'border gap' will remain consistent, while the images themselves need to dynamically adjust. C ...

Exploring bugs in Angular 14 through unit testing

After generating a new Angular project using the CLI ng new ..., I encountered an error when running npm test. The error appeared in the browser: and in the console as well: I haven't made any additional changes to the project. I tested with Node ve ...

Tips for positioning the avatar to the right along with other links?

I've encountered various methods to align text or an image to the right, but I'm having trouble aligning a basic avatar with some text. It used to work before I switched to material-ui-next, and now I can't seem to get them aligned properly. ...

What could be the reason for the gap that shows up when transitioning from one image to the

Check out my website at: This section discusses: https://i.sstatic.net/WTTxJ.gif At the bottom of the image, a 'gap' appears. Instead of using standard HTML img tags, I used Next.js Image like this: <Image src="https://ticket- ...

What is the best way to ensure a div container fills the entire height of the screen?

I am currently working on developing my very first iOS HTML5 app, which is a simple quote application. One challenge I am facing is how to set the height of my container div to match that of an iPhone screen. You can view the design I have so far on this j ...

The performance of Ionic 2 app views starts to lag

I'm currently working on an app with Ionic 2 that involves a timer functionality. Initially, the timer operates smoothly with instant updates. However, when viewed in Ionic View, the update frequency significantly decreases over time. Even when tested ...

How can I ensure that my SVG Image perfectly fits within the Parent wrapper div?

I've been grappling with the following code snippet. I'm struggling to make my SVG image fit within my container div. .img { margin: 0; padding: 0; overflow: hidden; width: 500px; position: relative; } img { top:0; ...

The space residing above my primary container division

I recently finished creating a basic web app, but I'm encountering an issue with the smallest media query where there is an unwanted top gap. Despite trying multiple methods and researching solutions online, I have not been able to resolve this proble ...

Tips for utilizing the JS attribute "offsetWidth" within Angular 4

I am attempting to retrieve the width of an element using JavaScript in my Angular application. document.getElementsByClassName("element")[0].offsetWidth; However, I keep encountering the following compilation error: Property 'offsetWidth' d ...

Hover effect for Bootstrap cards

I am working on a view where I display dynamically expanding cards upon hover. Below is the CSS code snippet: .card-deck .card { height:200px; width: 200px; transition: transform .1s; } .card-deck .card:hover { -ms-transform: scale(1. ...

Exploring jQuery Animation Effects: Enhancing Your Web Experience with Zoom In and Zoom Out

Is there a way to animate a logo using jQuery automatically upon page load? I want the image to zoom out, then zoom in and change to a different image. Can someone please assist me with this task? Below is the code snippet that I have: $(document).ready ...

Google Maps on Angular fails to load

I'm currently working on integrating AGM into my Ionic 2 project. app.module.ts ... import { AgmCoreModule } from '@agm/core'; import { DirectionsMapDirective } from '../components/directions-map'; @NgModule({ declarations: [ ...

CSS - Customizing the appearance of consecutive child divs

I'm struggling with adjusting the margin between two child divs when they follow each other. The current margin is set at 3rem, but I want it to be 1rem if both child containers have the class "narrow": Is there a way to achieve this without changing ...