Shifting an item between various components to modify its properties

I am currently working with an HTML table that looks like this:

<table>
  <tr>
    <td>Name</td>
    <td>Surname</td>
    <td>ID Number</td>
    <td>Edit</td>
  </tr>
  <tr *ngFor="let person of people | async">
    <td>{{person.name}}</td>
    <td>{{person.surname}}</td>
    <td>{{person.idNumber}}</td>
    <td>
      <input type="button" value="Change 
      (click)="saveSelectedPerson(person)">
    </td>
  </tr>
</table>

I am trying to create a function called (click)="saveSelectedPerson" so that when the button is clicked, the app navigates to another form where the user can edit the information of that specific person. I need to somehow pass that particular person into the ChangeInfoComponent, which is a separate component from this PreviewComponent.

I attempted creating an object in the PreviewComponent and assigning the properties from the clicked Person to it, but when I log that (this.goToPerson) object, it shows as undefined.

 saveSelectedPerson(p: Person) {
     console.log(p);
     console.log(this.goToPerson);
      this.goToPerson.name = p.name;
      this.goToPerson.surname = p.surname;
      this.goToPerson.idNumber = p.idNumber;
   }

What could I be doing wrong? Is my approach correct, or should sharing objects between components be done in a different way rather than through public component properties?

Answer №1

Utilize a shared service to store and access information pertaining to the chosen individual. The data can be saved using the saveSelectedPerson function within the service, and retrieved in the ChangeInfoComponent by utilizing the same service. For additional options and details, visit: https://angular.io/guide/component-interaction

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

Angular's '@angular/core/core' module does not have the exported member 'ɵɵFactoryDeclaration'

My Angular application was successfully compiled after running npm install. However, upon executing npm start, I encountered the following error: ERROR in node_modules/ng-apexcharts/lib/chart/chart.component.d.ts:57:21 - error TS2694: Namespace '&quo ...

Manage scss styles consistently across Angular projects with this Angular library designed to

In an effort to streamline my development process, I am looking to consolidate my commonly used styles that are defined in my Angular library. My goal is to easily leverage mixins, functions, variables, and more from my library in future projects. Previou ...

Testing Ag Grid's column headers using Jest and Angular CLI has proven challenging, as some columns from columnDefs remain elusive

Currently, I am using Jest and Angular Cli testing to validate column headers. In my attempt to replicate the process outlined on https://www.ag-grid.com/javascript-grid-testing-angular/#testing-grid-contents, I have encountered an issue where only 2 out ...

Tips for building an effective delete function in Angular for eliminating a single record from a table

I've been working on creating a method to delete an employee by their ID, and I've tried various approaches in my VS Code. Unfortunately, all of them have resulted in errors except for this specific method. Whenever I click on the delete button, ...

Encountering an error when invoking a web API controller from a service in Angular 2

I am currently following an Angular quick start tutorial that focuses on the Hero tutorial provided on the Angular2 website. The tutorial runs smoothly for me as it binds static array data and allows for CRUD operations. However, my goal now is to understa ...

Tips for Achieving Observable Synchronization

I've encountered a coding challenge that has led me to this code snippet: ngOnInit(): void { this.categories = this.categoryService.getCategories(); var example = this.categories.flatMap((categor) => categor.map((categories) = ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

Conceal the HTML element within a subscription

Currently, I am utilizing Angular and have a checkbox that I need to toggle visibility based on the response of an API call subscription. The issue lies in a delay when trying to hide the checkbox (as it is initially set to visible). My assumption is that ...

What is the process for defining the type of the context for an Apollo resolver?

I am facing an issue with my Apollo GraphQL subgraph where I need to define the type for the context argument in my resolvers. When creating a resolver, I tried setting the context type like this: interface Context { dataSources: { shopify: Shopify; ...

Why do I keep encountering the error "global is not defined" when using Angular with amazon-cognito-identity-js?

To start, run these commands in the command line: ng new sandbox cd .\sandbox\ ng serve Now, navigate to http://localhost:4200/. The application should be up and running. npm install --save amazon-cognito-identity-js In the file \src&bso ...

Encountering an error message about 'resolving symbol values statically' while building an Angular 2 project

Currently, I am utilizing an older module called angular-2-local-storage. The initialization process is as follows: const LOCAL_STORAGE_SERVICE_CONFIG_TOKEN: string = 'LOCAL_STORAGE_SERVICE_CONFIG'; export const LOCAL_STORAGE_SERVICE_CONFIG = ne ...

Encountering "Cannot write file" errors in VSCode after adding tsconfig exclude?

When I insert the exclude block into my tsconfig.json file like this: "exclude": ["angular-package-format-workspace"] I encounter the following errors in VSCode. These errors disappear once I remove the exclude block (However, the intended exclusion fu ...

Tips for utilizing withNavigation from react-navigation in a TypeScript environment

Currently, I am working on building an app using react-native, react-navigation, and typescript. The app consists of only two screens - HomeScreen and ConfigScreen, along with one component named GoToConfigButton. Here is the code for both screens: HomeSc ...

Strategies for preventing profanity in Typescript within Nuxt 2 implementation

implement user authorization functionality create the Auth class for authentication handling import type { Context } from '@nuxt/types'; export class Auth { public ctx: Context; constructor(ctx: Context) { t ...

An error of unknown token U was encountered in the JSON data starting at position 0

I am facing an issue with my MEAN Stack development. Currently, I have an Angular Form set up with proper values to create a company in the database using Node Express on the backend. However, I keep encountering an error stating that the JSON in Node is ...

Using Visual Studio Code Build Tasks in Harmony

The documentation for Visual Studio Code includes examples of tasks.json configurations that allow for either typescript compilation or markdown compilation, but does not provide clear instructions on how to achieve both simultaneously. Is there a way to ...

Transmitting data from Angular to .NET Core for seamless integration

I have been attempting to send an xls or any other file from my angular application to a .NET core controller, but none of my methods seem to work... Below is my component where I call my service upon button click: handleFileInput(file: FileList) { this. ...

Numerous mistakes detected in the TypeScript code

I've implemented the following class within an ASP.NET Core React application: import * as React from 'react'; interface MyInputProps { inputType: string; id: string; className: string; parentFunctio ...

Can the typography style of a material-ui TextField component be modified to incorporate a caption variant?

Currently diving into the world of material-ui for implementation in a Typescript React setup. Utilizing the code snippet below in a Dialog: ... <React.Fragment> <Typography variant="body1"> <Box mt={1}>Heading</Box> & ...

Angular 2 - update browsing history by replacing instead of adding to it

Is it possible to replace the history instead of pushing a new one in Angular 2's new router (rc.1)? For instance, suppose I am in a question list (/questions), then open a new modal in a new route (/questions/add). After adding a new question, I nav ...