After clicking on a specific card, it routes to a different component, but I also want to pass the data of that card to the new component. You can check out a working demo on StackBlitz here.
After clicking on a specific card, it routes to a different component, but I also want to pass the data of that card to the new component. You can check out a working demo on StackBlitz here.
To find the solution, consider utilizing a shared service
import { Injectable } from '@angular/core';
@Injectable()
export class SharedService {
selectedData;
constructor() { }
public setSelectedData(data) {
this.selectedData = data;
}
public getSelectedData(): any {
return this.selectedData;
}
}
dashboard.component.ts
import { Component, OnInit } from '@angular/core';
import { SharedService} from '../shared.service'
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
item;
constructor(private sharedService: SharedService) { }
ngOnInit() {
this.item = this.sharedService.getSelectedData();
}
}
table.component.ts
export class TableComponent implements OnInit {
items = [];
constructor(private myService: DataService, private router : Router, private sharedService: SharedService) {}
ngOnInit() {
this.myService.getItems()
.subscribe((res :[]) => this.items = res);
}
onItemClick(index) {
this.sharedService.setSelectedData(this.items[index]);
this.router.navigate(['/dashboard']);
}
}
table.component.html Invoke the onItemClick() with index
<mat-card (click)="onItemClick(i)" >
Check out this modified stackblitz demo
To securely share data across components, it is essential to utilize a shared service that allows for communication through setters and getters or observables.
If only the ID needs to be transferred, consider utilizing path parameters or query parameters. However, avoid passing excessive information through the URL to enhance security.
Implementation of SharedService:
export class SharedService {
public $cardDataSubject;
constructor() {
this.$cardDataSubject = new Subject();
}
setCardDetails(cardDetails) {
this.$cardDataSubject.next(cardDetails);
}
}
In the child component, subscribe to the shared service:
constructor(private sharedService: SharedServic) {
this.$cardDataSubject.subscribe(cardDetails => this.cardDetails = cardDetails);
}
Looking for a solution? Check out this link: How to pass data to Angular routed components?
Best Solution: implement the 3rd approach which involves using a service.
I am facing a scenario where I have two APIs with data containing similar ids but different values. The structure of the data is as follows: nights = { yearNo: 2014, monthNo: 7, countryId: 6162, countryNameGe: "რუსეთის ...
Here are my codes: export const LOAD_USERS = 'LOAD_USERS'; export const CREATE_USER = 'CREATE_USER'; export interface ACTION { type: string, payload: any } I am trying to limit the possible values for ACTION.type to either 'L ...
I have set up a next.js project with App Router and my file structure under the app folder looks like this: *some other files* . . user | [id] | | page.tsx | @users | | page.tsx | layout.tsx | page.tsx I am ...
Currently facing a hurdle and seeking advice I am developing an angular application that fetches data from an API and presents it on the page The service I am utilizing is named "Api Service" which uses HTTPClient to make API calls apiservice.service.ts ...
I encountered an issue with a third-party library where the class structure changed. Initially, it was defined as: export class Foo { field: X[]; …. } In my code, I was working with this type: print(foo.field) After updating to a new version, the c ...
Heads-up: Our Vue 2 setup is currently not utilizing the Composition API and we have no immediate plans to do so. This discussion focuses on vue-class-components. Query: The guide for vue-class-components mentions that we can employ a "normal" extends to ...
I have a project that utilizes Ionic 4 and Angular 8. The project requires integration with a payment gateway service, which I am accomplishing using the Ionic 4 InAppBrowser plugin to open the payment gateway site. This is how I've implemented it: ...
Currently, I am running some tests on a web service using Selenium. An issue I encountered is that certain elements get altered by Angular, making it difficult to interact with them as expected. One specific problem I faced was trying to modify text in wha ...
For my project, I am planning to receive icons/svgs as react components from a folder created by the backend. Additionally, there will be a WAMP (bonefish/autobahn) solution where I will be provided with the name of an icon to use (even though this may see ...
In my Angular-2 project, I am currently working on uploading files to the server. The code snippet provided below showcases how the file is uploaded: My main question pertains to linking these uploaded files to specific mongodb documents. How can I achiev ...
I went through all the tutorials on Angular Autocomplete using API to follow the steps. I implemented valueChanges to monitor the form control, used switchMap to send a new request with each keyword change, and then displayed the data in the autocomplete d ...
Hello, I am currently working on a project using Angular 7 and encountering the error TS2559: Type 'BookInterface[]' has no properties in common with type 'BookInterface'. Despite making changes to the code, the issue persists. Below is ...
What is the best way to pass a variable to the component sibling1? <parentComponent> <sibling1 [data]="parentData"></sibling1> </parentComponent> ...
I've been attempting to deploy a basic nodejs app on heroku, but I keep encountering the error mentioned above. Despite trying various solutions provided here, nothing seems to resolve the issue. Here's a summary of what I've attempted so fa ...
After spending a while pondering over this issue, I have yet to discover an elegant solution. My dilemma begins with an Enum: export enum Enum { A, B, C, } Next, there is an object that utilizes the Enum. While the structure is somewhat predi ...
I have a locally stored "region.json" file containing the following data: { "regionId":1, "region":"CAN" }, { "regionId":2, "region":"CEN" } Additionally, I have an "enviroment-app.component.ts" file structured as follows : import {Component, ...
I currently have Node and NPM installed with the most recent versions. When attempting to install Angular/cli, I encountered an error message stating: angular/cli and npm versions not compatible with current version of node. I am beginning to think that I ...
Currently, I am developing an angular application with ng9. I have a specific div where I need to display an avatar using an image fetched from the API in my component.ts file: .... export class HomeComponent implements OnInit { nextLaunch$: Observabl ...
I've built a React hooks application in TypeScript that utilizes multiple reducers and the context API. My goal is to maintain a single error state across all reducers which can be managed through the errorReducer. The issue arises when I try to upd ...
Since upgrading Angular, I have encountered an issue where fileReplacements no longer work with assets. Here is the code snippet that I am using: "fileReplacements": [ { "replace": "src/assets/scss/x.scss", ...