Manipulating classes in Angular based on array positions

Currently, I am working on developing a blog page with angular 5. In this setup, all blog posts are assigned the class:

col-md-4

However, my goal is to give the newest blog post (the first one shown on the page) a different class: col-md-12.

Below is the current HTML structure I have in place:

                <div *ngFor="let blog of blogs" class="blog-posts col-md-4">
      <div class="card">
                    <img class="blog-img" (click)="goToBlogDetailsPage(blog.sys.id)"src="{{blog.fields.blogimage.fields.file.url}}" class="rounded" alt="">
          <div class="info">
            <h3 class="author">{{blog.fields.authorInfo}}</h3>
                            <h2  (click)="goToBlogDetailsPage(blog.sys.id)" class="title">{{blog.fields.title}}</h2>
            <p class="short-desc" > {{blog.fields.desciption}}</p>
            <img (click)="goToBlogDetailsPage(blog.sys.id)" class="sml-logo" src="/assets/img/logos/logo_blue.png" alt="">

            <!-- <button (click)="goToBlogDetailsPage(blog.sys.id)"class="btn btn-info">Open Blog Post</button> -->
          </div>
        </div>
                    </div>
                </div>
            </div>

I've attempted using the index of the array[0] to adjust the styling for only the initial item, but have not been successful so far. Any ideas or suggestions on how to achieve this?

Answer №1

To dynamically assign a CSS class based on index, use the index and ternary operator in Angular

<div *ngFor="let blog of blogs; let ind = index" class="blog-posts"
  [ngClass]="ind === 0 ? 'col-md-12' : 'col-md-4'" >

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

What is the best way to integrate Angular into my existing *ngFor loop?

Within this code segment, I am attempting to display testRecords on the screen, which are test records. However, even though there exist test records, the ngFor loop is not being entered. {{testRecords}} <div class="row mb-2" *ngFor="let ...

Retrieve combination values through an AJAX request using ExtJS

My UI is developed using ExtJS, and I have a specific set of tasks that need to be executed when the page loads: Initiate an ajax call to the server to fetch a HashMap. Create a combobox within the main Panel on the page. var combo = Ext.create(' ...

Ways to layer two divs on each other and ensure button functionality is maintained

In the process of developing a ReactJS project, I encountered the challenge of overlapping my search bar autocomplete data with the result div located directly below it. For a more detailed understanding, please take a look at the provided image. Here&apo ...

Displaying PDF content in a new browser tab by utilizing JavaScript

Currently, I am utilizing the struts2 framework alongside dojo for the UI. My goal is to display a PDF in a new browser window. The PDF inputstream is obtained from the server through a standard AJAX call using the GET method (not utilizing a Dojo AJAX cal ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Delay in loading Jquery accordion due to value binding

I've noticed that my jquery accordion takes a significant amount of time to collapse when the page initially loads. After some investigation, I realized that the issue lies in the fact that there are numerous multiselect listboxes within the accordio ...

Issue with displaying Angular chart only resolves after resizing window following routing updates

Hey there! I'm having trouble getting my chart to show up after adding routing to my 3 tabs. Previously, without routing, everything worked fine. Now, the graph only appears after resizing the window. The chart is using PrimeNG chart with the Chart.js ...

What is the best way to assign src and alt attributes to an image tag using JavaScript?

As I continue to learn JavaScript, please bear with me as I navigate through my current level of understanding. I am working on a gallery that opens a modal when an image is clicked. I have successfully gathered all the image sources into an array and used ...

Pinterest's "Pin it" button causing issues with the 'back' function in Internet Explorer

Recently, I discovered a problem with the "Pin it" button for Pinterest in Internet Explorer (v9 at least). It seems that this button is causing issues with the 'back' functionality in the browser. When right-clicking on it, an entry like '& ...

What is the best way to pre-fetch data using axios in Vue?

My app requires offline functionality for drivers operating in remote areas with limited internet access. To address this, I aim to pre-download all necessary data using Axios requests when an internet connection is available. This way, the app can retriev ...

Please convert the code to async/await format and modify the output structure as specified

const getWorkoutPlan = async (plan) => { let workoutPlan = {}; for (let day in plan) { workoutPlan[day] = await Promise.all( Object.keys(plan[day]).map(async (muscle) => { const query = format("select * from %I where id in (%L) ...

Node.js data remains unchanged

I am currently working on a project involving Angular, Node.js, and MySQL. I have created a simple CRUD application where users can add and update data. One of the requirements is that changes made to the data should be reflected in the list grid immediate ...

What is the best way to increment a variable by one when a button image is clicked?

Is there a way to increase a variable by +1 when clicking on an image button? ~John ...

How can you display a set of components in React using TypeScript?

I'm currently working on rendering an array of JSX Components. I've identified two possible methods to achieve this. Method one (current approach): Create the array as an object type that stores the component properties and then build the JSX co ...

Submit the scaled-down form image to the server

When attempting to upload a resized image to the server, an error stating "Required MultipartFile parameter 'file' is not present" occurs. Interestingly, this error only appears when trying to upload the resized image, as uploading the original f ...

Interpret a variety of date formats using date-fns

Here is a code snippet that I am working with: function checkDate(value: string) { return isBefore( parse(value, 'dd-MM-yyyy', new Date()), sub(new Date(), { days: 1 }) ); } const result = checkDate('31-12-2020'); In the p ...

Troubleshooting: Why is my CSS background image URL not displaying?

I'm struggling to understand why the background image is not showing up. However, when I use content:url(), it does appear. The issue with using content:url() is that I can't control the size of the image. Below, you will find the complete code f ...

Collapse all Angular Sidemenu subitems at once

I have a Sidemenu with items and subitems. Currently, the subitems open by default but when I close one, it closes all the items together. I want to modify it so that only that specific item closes. The reopening functionality is working fine but I need th ...

Unable to pass props to component data using Vue framework

I'm facing an issue with passing props in my component to the same component's data object. For some reason, I'm only receiving null values. Does anyone have any suggestions on how I should approach this problem? It seems like my Vue compone ...

Top technique for mirroring a website

Recently, I created a directory for a client's website. The site is fully operational, but the client would like a replicated version for testing and learning purposes. To create a full replica of his website, it will require hours of work to change ...