Introducing a detailed model showcasing information sourced from an array of objects within Angular 14

I'm struggling to showcase detailed models for various elements in my project. In essence, I have a collection of cards that hold diverse information data. The objective is simple: upon clicking a button to reveal more details about a selected card, a modal should appear displaying its name, description, and image based on the specific card chosen. The challenge lies in the fact that all this information is sourced from an array stored in a separate .ts file.

Initially, I defined a class and an array of elements which would be linked to a property of that class type in the AnimalsList.component.ts file:

export class Animals{
  title: string
  desc: string
}

export var arrayAnimals = [
  {
    name: "Cat!",
    desc: "Here comes more text...",
  },

Within the AnimalsList.component.ts file, I declared the property 'animals' and assigned the class as well as the array to it:

animals: Animals[]
ngOnInit(): void {
  this.animals = arrayAnimals
}

The AnimalsList.component.html file features an ngFor loop to display the list of animals and a button to trigger the modal:

<div *ngFor="let animal of animals"> 
 <p>{{ animal.name }} </p>
 <p>{{ animal.desc}} </p>
 <button (click)="openAnimalModal(animal)">Detail</button>
</div>

My attempts to implement scripts utilizing Ng Bootstrap library and jQuery were unsuccessful, as they only returned raw HTML content (e.g., the "Testing" text).

In the AnimalModal.component.ts file:

  @Input(animals): Animals;
  constructor(
    public activeModal: NgbActiveModal
  ){}

And in the AnimalModal.component.html file:

  <p>Testing!</p>
  <p>{{ animals.name}}</p>
  <p>{{ animals.desc }}</p>

To launch the modal and attempt to pass data to it, here's how the AnimalsList.component.ts script for the modal looks like:

  openAnimalsModal(animals: Animals){
    const modalRef = this.modalService.open(AnimalModalComponent);
    modalRef.componentInstance.animals= animals;
  }

Any assistance would be greatly appreciated. Thank you in advance!

Answer №1

Angular Material makes building user interfaces simple. Here's a guide to get you started:

...
constructor(private modal:MatDialog){}
...
openAnimalsModal(animals: Animals){
  this.modal.open(AnimalModalComponent, {
    data: { animals }})
}

Next, within your AnimalModalComponent, follow these steps:

import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';

...

constructor(
  public dialogRef: MatDialogRef<AnimalModalComponent>,
  @Inject( MAT_DIALOG_DATA ) public data: DialogData
){}

ngOnInit(): void {
    console.log(this.data)
}

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

Updating an array using `setState` does not result in the array being updated

I created a component that uses the .map() method to render an array of students and has a button to shuffle and update the display. However, I'm facing an issue where the display does not update every time I click the button. const Home: NextPage = ...

Why is it necessary for the required type of a function parameter to be able to be assigned to

From Optional to Required Type const testFunc = (func: (param: number) => void): void => { func(3); }; testFunc((a?: number) => { console.log(a); }); From Required to Optional Type const testFunc = (func?: (param: number) => void): void = ...

How to create a beautiful grid layout using @angular/flex-layout, @angular/material, and mat-card to showcase stunning images

I have created a basic layout that looks like this: Cell 0 Cell 1 Cell 2 <!-- these are not to appear in actual render __________________________________ | Image 0 | Image 1 | Image 2 | |----------| Image 1 |----------- | text here| ...

Issue with React/Next.js input field rejecting commas

I'm experiencing a problem with an input field in my React application that was developed using Next.js. The issue arises specifically when using an iPad device, where the input field behaves inconsistently when dealing with commas. When using deskto ...

Using keyof on an indexed property within a generic type in Typescript does not effectively limit the available options

Imagine having an interface structure like this: export interface IHasIO { inputs: { [key: string]: string }, outputs: { [key: string]: string } } The goal is to develop a function that adheres to this interface as a generic type and ensur ...

Utilizing ngFor in Angular to dynamically refer to named variables within ngIf

I am looking to access the input element within the ngIf directive in order to check if it currently contains a specific value. The issue lies with the code inside the ngIf statement. <span *ngFor="let item of items;let i=index"> <input t ...

Is there a way to access the filtered or organized rows in the Quasar Q-Table?

I have encountered a roadblock in my project. Despite installing Quasar version 2.0.0, I realized that it lacks a property to access the filtered or sorted rows. Previous versions of q-table had a computedRows property which was missing in the latest ver ...

Ways to attain a similar format - input map

After pressing the save button, I am aiming to achieve the following effect: "timeTable":{ "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"18:20&q ...

Angular 7 application encountering error 500 with URL Rewrite Module on IIS7 server

I am facing an issue with my Angular 7 application hosted on IIS 7. The app should be accessible at https://example.com/fitcbooks/web. It was working fine initially, but suddenly stopped without any clear reason. Despite the fact that URL Rewrite on the se ...

Allowing Cross-Origin requests in Spring BootLet's configure the

Every time I attempt to communicate with my backend using my Angular client, I encounter a common error message when trying to perform POST, PUT, and DELETE requests: Access to XMLHttpRequest at 'http://localhost:8087/categories' from origin &ap ...

Tips on using the `IsEqual` function to develop a tool that verifies the similarity of different data types

When working with TypeScript, I often utilize the type-fest npm package in my coding. For documentation purposes, I sometimes like to assert that two types are either equal or unequal. Here is an example: const b: IsEqual<{a: 1}, {a: 1}> = true; con ...

Automatically generate user clicks within primeng pagination

I am implementing primeng's p-dataview component with pagination to display user test questions. <p-dataView [value]="userTestQuestions" [paginator]="true" [rows]="1" [totalRecords]="totalTestQuestions"> <ng-template let-question pTempl ...

Encountering 404 errors when reloading routes on an Angular Azure static web app

After deploying my Angular app on Azure static web app, I encountered an error. Whenever I try to redirect to certain routes, it returns a 404 error. However, if I navigate from one route to another within the app, everything works fine. I have attempted t ...

Sharing variables between parent and child components in Angular 2 Dart using router

How can I effectively share variables between a parent component with a router and a child component while keeping them updated through bindings? Here is a snippet of the code: app_component.dart: import 'package:angular2/core.dart'; ...

Angular 5 offers the ability to incorporate dynamic checkbox input into your application

Here is my code snippet: <input [type]="'checkbox'" [(ngModel)]="inputValue"> <p>Value: {{ inputValue }}</p> I'm puzzled as to why the value in inputValue remains unchanged. Can anyone shed light on this? I am unable to ...

Tips on handling multiple Redux toolkit CreateApi interceptors

I came across this solution here for implementing a reAuth baseQuery in Redux Toolkit. I have several backend services that all use the same refresh token concept. Is there a way to create a single baseQueryAuth function that can be used by multiple creat ...

The headerStyle does not have any impact on the header component in React-Native

Currently diving into React-Native with Typescript and working on a project. Encountered a bug where the header color isn't changing as expected. Any help or insight would be greatly appreciated! -Viggo index.tsx import React, { Component } from & ...

How to hide ion-input when typing in Ionic 3

I am looking for a way to customize the output of an ion-input field. Instead of displaying what the user is typing, I want to only show the result of a function called formatNum when ngModelChange is triggered. This means that the actual input from the ...

Redirecting to child routes based on conditions

I encountered a situation where I need to lazily load child routes and display them conditionally: const routes: Routes = [ { path: '', component: MainComponent, canActivate: [AuthGuard], children: [ { path: &apos ...

crafting connections in 3D using TypeORM (ORM)

I attempted to construct a database schema involving users, groups, documents, and permissions. Users can be part of multiple groups Groups can have multiple users Users can possess permissions for documents Groups can have permissions for documents Perm ...