Is there a way to access the final child element within a mat-accordion component using Material-UI and Angular 8?

I have a mat-accordion element with multiple expansion panels that are generated dynamically. How can I programmatically select and expand the last mat-expansion-panel element?

<mat-accordion>
  <mat-expansion-panel>
    text 0
  </mat-expansion-panel>
  <mat-expansion-panel>
    text 1
  </mat-expansion-panel>
  <mat-expansion-panel>
    text 2
  </mat-expansion-panel>
...
</mat-accordion>
<button (click)="addNewItemAndExpandIt()">Add item and expand it</button>

Answer №1

If you want to utilize ngFor with the last directive, here's a sample code snippet:

<mat-accordion>
  <mat-expansion-panel *ngFor="let panel of expansionPanels; last as isLast;" [expanded]="isLast">
    text 0
  </mat-expansion-panel>
...
</mat-accordion>

UPDATE 1

DEMO

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

Utilizing the JavaScript Array.find() method to ensure accurate arithmetic calculations within an array of objects

I have a simple commission calculation method that I need help with. I am trying to use the Array.find method to return the calculated result from the percents array. The issue arises when I input a price of 30, as it calculates based on the previous objec ...

Navigating Through Angular Components

I have a layout with 3 components arranged like this: <app-header></app-header> <app-body></app-body> <app-footer></app-footer> However, I want to change the positioning of the footer and body as shown below: <app-he ...

What is the reason behind the mandatory credentials option for the CredentialsProvider?

When using NextAuth.js with a custom sign in page, some code examples for the credentials provider do not include the credentials option in the CredentialsProvider. According to the documentation (here), the credentials option is meant to automatically "ge ...

What is the reason behind Typescript errors vanishing after including onchange in the code?

When using VSCode with appropriate settings enabled, the error would be displayed in the following .html file: <!DOCTYPE html> <html> <body> <div> <select> </select> </div> <script&g ...

The nest build process encounters errors related to TypeScript in the @nestjs/config package, causing it

Encountering several issues related to @nestjs/config, causing the npm build command to fail. However, npm run start:dev is still functional despite displaying errors. See below for screenshots of the errors and environment: https://i.sstatic.net/Wxkkn.png ...

Refine the category based on a specified key

Currently, I am in the process of developing a React Hook using TypeScript. In this hook, I am passing both a key and a value that will be associated with this key as arguments. My objective is to constrain the type of the value based on the specified key. ...

When encountering the error message "Warning Invalid prop `color` with value `inherit` given to `ForwardRef(TextField)`, make sure to provide either "primary" or "secondary" as valid options

Recently, I encountered this warning message: Warning: Failed prop type: The prop color value of inherit is invalid for the component ForwardRef(TextField). It should be either primary or secondary. To address this issue, I attempted changing the color f ...

Typescript: Determining the return type of a function based on its input parameters

I've been working on a function that takes another function as a parameter. My goal is to return a generic interface based on the return type of the function passed in as a parameter. function doSomething <T>(values: Whatever[], getter: (whatev ...

The inner map function isn't being executed in Rxjs

I have written some code that utilizes two pipes to load a product instance along with its playlist. In case there is an error while loading the playlist, the property is set to null: getProduct(productId: number): Observable<ProductDTO> { retur ...

Choose the underline effect for your style

I'm attempting to utilize styled-components in order to style the underline of a select component. Although I was able to successfully style the underline of an input component, it seems that the same code does not work for the select component. impo ...

Is it possible to obtain Literal types for object keys dynamically in typescript?

I am looking to extract the type of object keys. Below is a generic function for objects with keys as strings: type GenericInput = { [key:string]: {value:string,type:HTMLInputTypeAttribute,placeholder:string,min?:number,max?:number,required?:boolean, err ...

The type 'Promise<UserCredential>' cannot be assigned to

import React, {createContext, useContext, useEffect, useState, ReactNode} from "react"; import { auth } from '../utils/init-firebase'; import { createUserWithEmailAndPassword } from "firebase/auth" type ButtonProps = { ch ...

Can you explain the functionality of the container prop within the Drawer component of material-ui?

While delving into the official documentation for material-ui's Drawer component, I stumbled upon a mysterious container prop that is not explicitly mentioned in the API documentation. Could this possibly be one of the inherent props of a React compon ...

Building NextJS with Typescript encountered an error while using @auth0/nextjs-auth0

I am currently facing an issue while trying to build my NextJS application using Typescript. The problem lies with the auth0/nextjs-auth0 package, causing persistent errors during installation. One specific error can be found at the following link: https: ...

Exploring the options variables within CLI commander Js action

As a newcomer to developing CLI apps, I've chosen to work with ts-node and commander. However, I'm currently facing a challenge in understanding how to access the options that users pass into my command action. program .version(version) .nam ...

How to generate an interactive text-box using Angular 8 and connecting the input to the component during form submission

When I attempt to add a dynamic text box after clicking a button, the text box is successfully added. However, I am facing an issue where I am unable to retrieve all the values (values of dynamically added text boxes) when the form is submitted. It seems t ...

Utilizing a string as an argument in a function and dynamically assigning it as a key name in object.assign

Within my Angular 5 app written in TypeScript, I have a method in a service that requires two arguments: an event object and a string serving as the key for an object stored in the browser's web storage. This method is responsible for assigning a new ...

The function in Angular 5/Typescript disappears when attempting to call it from within another function

After importing D3 into my component, I encounter an issue when trying to assign a layout to the D3.layout property. Strangely, although the layout property is present in the console output of my D3 object, it seems to be unknown when I attempt to call i ...

Encountering a Issue with Http module in Angular

When attempting to call my API using HttpModule, an error is being thrown upon starting the server (please refer to the screenshot). Error Image The error arises when I try to make a call to the API from the service using Http.post method. Here is my app ...

Creating a custom Material UI element with unique starting characteristics

As a newcomer to React and MUI, I'm looking to pass an icon as a prop to another component and have the ability to programmatically set its color before rendering based on certain conditions like the toggle status of a button. Picture this example sce ...