Encountering difficulties transferring data from a parent to a child component within Angular 9

In my current situation, I am passing a variable's value up to its grand-parent component using EventEmitter successfully. However, once the value reaches the grand-parent, I want to pass it to another child component within that grand-parent. I have tried using @input name:string in the child component to achieve this, but for some reason, I am unable to access the name variable in that child component.

The parent component setup looks like this:

//html code
<personal-section (childName)="getName($event)"></personal-section>
<profile-section [name]="name"></name>

//inside profile.ts
 getName($event) {
    console.log(this.name);       
    this.name= $event;
  }

And the child component looks like this:

 @Input() name: any;
 ngOnChanges() {
    if (this.name) {
      console.log('users name is ', this.name);    
    }
  }

Although when I trigger a click event in the grand-child component, the value successfully makes its way up to the grand-parent. However, I am encountering issues in accessing it in the final destination - the other child component.

I appreciate any help provided in advance.

Answer №1

Swapping out the getName($event) in the controller with getName(event) is simply a matter of following common practice.

Furthermore, if your goal is to only pass the name variable from one child component to another without involving the parent, you can achieve this using a custom variable. Give this a try:

Parent template

<personal-section (childName)="profileSection1.setName($event)"></personal-section>
<profile-section #profileSection1></name>

The @Input decorator for the name property in the 'profile section' component can then be removed.

Profile section component

export class ProfileSectionComponent implements OnInit {
  name: any;

  public setName(name: any) {
    this.name = name;
    console.log('User name is ', this.name);
  }
  .
  .
}

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 an object property for @Input binding in Angular: A step-by-step guide

I am currently delving into Angular and Ionic Frameworks, honing my skills through practice. I'm encountering an issue with a basic @Input test, where I am trying to iterate through an array of Tab Pages and then display each tab using <ion-tab> ...

Unable to retrieve a boolean with the statement "return of(false)"

My objective is to retrieve data, store it, and return either true or false based on the operation outcome. Initially, I attempted to make the call and then use return of(true) to create an observable. The method I have is as follows. setValidations(): Ob ...

Named functions in Typescript within functional components are the best practice for improving

How can I implement handleFoo using MyType['foo']? type MyType { foo: () => void } const Comp: React.FunctionComponent<{}> = () => { function handleFoo() {} return ... } I'm looking for a solution that doesn't inv ...

Steps to Transform String Array into Prisma Query Select Statement

I have a requirement to dynamically select Prisma columns based on client input: ['id', 'createdAt', 'updatedAt', 'Order.id', 'Order.Item.id', 'Order.Item.desc'] The desired format for selection ...

Props used in styled components are effective, although they may trigger a warning message stating, "Warning: Received `true` for a non-boolean attribute `cen`."

Caution: A non-boolean attribute "cen" received a value of "true". If you intend to render it in the DOM, provide a string instead: cen="true" or cen={value.toString()}. While using Props in Styled-Component with TypeScript and Material-UI, everything func ...

When using TypeScript in Angular, the "debugger;" statement may encounter issues in the QA environment

Currently engaged in developing an angular application that involves working with multiple environments, I am encountering difficulties while attempting to set up debugging points using debugger; For compiling node_modules and running the server, I use th ...

When a user clicks on the download link, it redirects them to the homepage in Angular

When using Angular 6 and the downloadFile method to download an Excel sheet from the WebAPI, everything runs smoothly. A dialog box opens up asking to save the file on the drive, but then it unexpectedly navigates me back to the home page. This redirects ...

Using Rollup alongside Typescript to handle absolute imports for type declarations

In the process of creating a React component library, the project structure resembles the following: src/ components/ utils/ hooks/ Currently, there is an attempt to generate type files (.d.ts) using rollup. The types are successfully generated, ...

There is a gap found in the span element in Angular, but this gap is not seen in

Comparing two component templates, one in AngularJS and the other in modern Angular: <span ng-repeat="status in statusInformation.statusList | filter : whereFilter" class="show-on-hover"> <span class="app-icon ap ...

Tips for notifying the user about incorrect login credentials in Ionic 3

I'm attempting to implement a notification system using showAlert to inform users when they enter an incorrect email or password, but I'm having difficulty achieving this using try and catch statements Would it be feasible for me to use try and ...

Angular application experiences issues with persistent login when using Cypress.io

Recently, I decided to follow a tutorial provided by Auth0 (https://auth0.com/blog/end-to-end-testing-with-cypress-and-auth0/) in order to utilize Cypress.io. However, despite my efforts, I have been unable to retain a successful sign-in with Cypress. Ini ...

Adding a total property at the row level in JavaScript

Here is a JavaScript array that I need help with: [{ Year:2000, Jan:1, Feb: }, {Year:2001, Jan:-1, Feb:0.34 }] I want to calculate the total of Jan and Feb for each entry in the existing array and add it as a new property. For example: [{ Year:2000, Ja ...

Exploring Query String Retrieval and Data Fetching in Ionic 2 using Angular 4's $

I am struggling to find the information I need, so I'm reaching out for help on how to send a request to a JSON api with parameters encoded in JavaScript. It is crucial that the parameters are properly encoded because the query could contain special ...

Encountering a problem when trying to use event.target.value in an Angular TypeScript application

Here is the code from my app.component.html : <h1>Password Generator</h1> <div> <label>Length</label> </div> <input (input)="onChangeLength($event.target.value)"/> <div> <div> <input ...

Incorporating a parameter into a <div> only when the parameter holds a value

Being new to web development, I have a rather basic query. If the datainfo prop variable is not empty, I'd like to include a data attribute in the div tag. <div style={{height: props.height - handleHeight()}} data={datainfo ? datainfo : ""}> C ...

What are the ways in which I can utilize the private or public keyword in TypeScript?

Having issues specifying private or public properties in my TypeScript files (tsx/ts files) for a React project. The IDE being used is WebStorm 2021.3. TypeScript version in use is 4.5.4. Attempts were made to adjust some properties in the tsconfig.json ...

Help with Material-UI: Passing unique props to a custom TreeItem component

I am trying to pass an argument category to the component CustomTreeItem which uses TreeItemContent. Documentation: https://mui.com/ru/api/tree-item/ import TreeItem, { TreeItemProps, useTreeItem, TreeItemContentProps, } from '@material-ui/lab ...

How can I emphasize the React Material UI TextField "Cell" within a React Material UI Table?

Currently, I am working on a project using React Material UI along with TypeScript. In one part of the application, there is a Material UI Table that includes a column of Material TextFields in each row. The goal is to highlight the entire table cell when ...

How to Trigger an Angular 9 Function Using jQuery

Is there a way to trigger a function within an Angular 9 component using jQuery? I have an Angular 9 application that allows the integration of small jQuery plugins. I now need to make a basic function in the Angular app accessible to these jQuery plugins ...

The object has been defined, but the specific property is returning as undefined

I have defined a class in the following way: export class User{ private _name:string constructor(){} get name():string{ return this._name } set name(val:string){ this._name = val } } Now, in my login code, I am ...