Is it possible to have the first accordion in Angular2 automatically open without needing to click on it?

Hello everyone, I am seeking assistance with solving a problem I am having. I am using an *ngFor loop for an accordion and I would like the first accordion to be open by default. Can anyone provide guidance on how to accomplish this? Below is my Angular2 code snippet.

<div class="today col-lg-9 col-md-12 col-sm-12 col-xs-12">
        <md-card *ngFor="let value of timer_entries.entries" style="padding:0px !important;margin-bottom: 10px;margin-top: 15px;">
            <div class="accordion-section">
                <accordion>
                    <accordion-group #group>
                        <div accordion-heading>
                            {{value[0]}}
                            <i class="pull-right glyphicon glyphicon-triangle-right" [ngClass]="{'closeArrow': group?.isOpen, 'openArrow': !group?.isOpen}"></i>
                            <span class="pull-right padding-right-20 color-primary">{{value[2]}}</span>
                        </div>
                        <table border="0" cellpadding="0" cellspacing="0" st-table="rowCollection" class="table table-hover" layout-align="center">
                            <tbody class="table_body" layout-align="center">
                                <tr class="table-content wd_table_row" *ngFor="let d of value[1]">
                                    <td class="date-cell">{{d.times[0].date | date:'dd/MM/yyyy'}}</td>
                                    <td class="location-cell">{{d.department}}
                                    </td>
                                    <td class="category-cell">{{d.category}}</td>
                                    <td class="subcategory-cell">{{d.subcategory}}</td>
                                    <td class="client-cell">{{d.client}}</td>
                                    <td class="project-cell">{{d.project}}</td>
                                    <td class="duration-cell">{{d.total_hours}}</td>
                                    <td class="icon-cell-one" style="cursor:pointer;">
                                        <img class="wd-reload-icon" style="width:16px;" src="/images/icons/refreshIcon.svg" alt="reload">
                                    </td>
                                    <td class="icon-cell-one" style="cursor:pointer;" (click)="editModal.show()">
                                        <img style="width:16px;" src="/images/icons/pencil-new.svg" alt="edit">
                                    </td>
                                    <td class="icon-cell-two" style="cursor:pointer;" (click)="deleteModal.show()">
                                        <img style="width:16px;" src="/images/icons/delete-new.svg" alt="delete">
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </accordion-group>
                </accordion>
            </div>
        </md-card>

Answer №1

Give this a shot.

Include an index in your loop

<md-card *ngFor="let value of timer_entries.entries; let i = index" style="padding:0px !important;margin-bottom: 10px;margin-top: 15px;">

You can now apply the open class to the accordion at the 0th index like so

[ngClass]="{'open':(i==0)}"

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

An error occurred due to an unexpected token found in the JsonForms custom-renderer props

I found inspiration in an example from this website to develop a custom renderer for JsonForms using Vue: However, as I implement this example in my .vue file within the script tags, I encounter an error UnexpectedToken right after declaring the props. It ...

How can I tally all the repeated boolean values in an Angular HTML page?

Is it possible to calculate the total number of looped boolean values in an Angular HTML page? this.items = [{ id:1, isUpdate: false }, { id:2, isUpdate: true }, { id:3, isUpdate: true } If any of the isUpdate values are ...

Issue with MomentJs: incorrect month displayed

Momentjs is displaying the wrong month. const time = moment("2300","hhmm").format("HH:mm"); const weekDay = moment().weekday(2).format("DD/MM/YYYY"); const notification = moment(weekDay + " " + time, "DD/HH/YYYY HH:mm"); console.log(notification); Expect ...

A More Straightforward Approach to Unsubscribing from Observables in Angular 7

Is there a way to simplify the process of automatically unsubscribing from Observables when a component is destroyed using takeUntil? It becomes tedious having to repeat the same code in multiple components. I am looking for a solution that allows me to a ...

At what point is the ngOnInit function invoked?

Here is the code snippet I am working with: <div *ngIf="hotels$ | async as hotels; else loadGif "> <div *ngFor="let hotel of hotels | hotelsFilter: _filteredType; first as f; index as i " appInit [hotel]="hotel " [first]="f "> < ...

What is the reason behind TypeScript permitting the assignment of a class instance to a variable that is of a different type?

When examining the given code, it raises a question about why the TypeScript compiler permits the assignment const c1: I = new C();. The issue arises when the function call c1.z(args); results in an error due to the absence of the first property in the a ...

Step-by-step guide on implementing a draggable component for selecting the year using React

I am looking to develop a draggable component in React without relying on any third-party library. Below, I have provided an image depicting how the component might look. Currently, my component code appears as follows: import React from 'react'; ...

Encountering a problem in React.js and Typescript involving the spread operator that is causing an error

Could someone assist me with my current predicament? I attempted to work with TypeScript and utilize the useReducer hook. const initialState = { a: "a" }; const [state, dispatch] = useReducer(reducer, {...initialState}); I keep encountering an error ...

Unable to locate the type definition file for 'zen-observable'

While working with Ionic 3, I decided to use the AWS default template and encountered an error: > ionic start app0 aws // aws is a default starting app in Ionic3 > cd app0 > ionic serve However, when I tried to serve the application, I received ...

Is it Possible to Expand Union Type Aliases in TypeScript?

One challenge I'm facing is limiting string fields to specific values during compile time, while also allowing these values to be extendable. Let's explore a simplified example: type Foobar = 'FOO' | 'BAR'; interface SomeInt ...

Angular error TS2531: Object may be null

Within my Component.html file, I have an input field set up like this: <input type="text" (change) = "setNewUserName($event.target.value)"/> The code within the component.ts file looks like this: import { Component } from "@ ...

Intercepting HTTP requests in Angular, but not making any changes to the

Working on Angular 13, I am trying to attach a JWT token to the headers in order to access a restricted route on the backend. However, after inspecting the backend, it seems that the JwtInterceptor is not modifying the HTTP request headers. I have included ...

Ensuring uniformity in picture size while uploading images in Angular by resizing and including white borders

Currently, I am working with pictograms and I am looking to showcase all the pictograms that the user has in both grid list and grid tiles formats. However, the challenge lies in the fact that the aspect ratio of the pictograms varies significantly depen ...

I'd like to know how to retrieve a total count of all the documents within a Firebase collection using Angular

My code currently fetches documents from a collection, but it's only bringing back 15 at a time (from what I can gather). This is causing an issue as I need to accurately determine the total number of documents in the collection for a program I'm ...

Changes in tabs are discarded when switching between them within Material UI Tabs

I have been experiencing an issue with the Material UI tab component where changes made in tabs are discarded when switching between them. It seems that after switching, the tabs are rendered again from scratch. For example, let's say I have a textFie ...

What is the best way to wait for a series of subscriptions to complete?

I am currently facing challenges with Observables while working on a complex REST API query function that involves intricate logic and multiple requests and responses. Although I have already written numerous functions with subscriptions like the ones bel ...

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 ...

Controlling the reactivity of the ngrx signal store in a component class

Following the upgrade to Angular v17, I find myself in the process of rewriting a legacy component using the traditional ngrx (observables based) approach alongside the new ngrx/signals store. I have defined some selectors in the following manner: withCom ...

Creating a setup in TypeScript to enable imports between CommonJS and ES modules (for node-fetch and Express)

I'm facing a challenge in trying to integrate two libraries into a single project: fetch-node, an ES module, and Express, which follows the CommonJS format. The issue arises from needing to import fetch-node using: import fetch from 'node-fetch&a ...

Function in Angular that provides the elementId when hovering over it

Currently, I am working on an angular project that involves creating a wiki window. Basically, when any element is hovered over with the mouse, its definition will appear inside the wiki window. I am wondering if it would be possible to create a global fun ...