I am attempting to pass input values from a parent component to a child component in order to display them, utilizing the @input decorator

How can I pass input values from a form in the parent component to be displayed in the child component using @Input?

Login Form (Parent):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div class="form">
        <h2 id="registration">Login Form</h2>
        <form (ngSubmit)="navToDisplayForm()" #f="ngForm">

            <div>
                <label for="firstname">First Name</label>
                <input type="text" id="firstname" placeholder="Your name.." name="firstname"
                    [(ngModel)]='userModel.firstnamedetail' required>
                


                <label for="middlename">Middle Name</label>
                <br>
                <input type="text" id="middlename" placeholder="Your middlename.." name="middlename"
                    [(ngModel)]='userModel.middlenamedetail' required>
                
                <br>
                <label for="lastname">Last Name</label>
                <input type="text" id="lastname" placeholder="Your lastname.." name="lastname"
                    [(ngModel)]='userModel.lastnamedetail' required>
                


            </div>


            <br><br>

            <button class="btn btn-info" type="submit" routerLink="/userdata" (click)="navToDisplayForm()"
                value="Submit" id="btn">Submit</button>
            <p>Not Registered? <span>Create Account</span></p>
        </form>


    </div>
    

</body>
<app-userdata [childData]="sendToChild"></app-userdata>


</html>

Login Form TS (Parent):

// Angular code here

User Data TS (Child):

// Angular code here
 

User Data HTML (Child):

 <!-- Angular HTML structure -->

Answer №1

One issue to address is that you are binding the wrong data by sending data as firstnamedetail and attempting to use it for firstnameToDisplay. This kind of binding won't work correctly. To fix this, you can update your binding in userData.ts with @Input() childData: any; and make adjustments to your userData.html with the following snippet.

<div>
  <form class="display">
    <p>First Name: {{ childData?.firstnamedetail ?? '' }}</p>
    <p>Middle Name: {{ childData?.middlenamedetail ?? '' }}</p>
    <p>Last Name :{{ childData?.lastnamedetail ?? '' }}</p>
  </form>
</div>

The ?. operator is known as optional chaining, which will return undefined if childData.property is undefined. In case of undefined, the ?? (Nullish coalescing operator) will display an empty string.

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

After installing a new npm package, certain components within my project suddenly stopped functioning properly, specifically those using

I recently attempted to integrate the ngx-markdown-editor from this source into my project. However, upon installing the npm package for this editor, I encountered an issue where one of my other components located in a completely different part of the proj ...

Utilizing Angular 8 (TypeScript) and .NET Core to send a GET request with dates as a parameter

I attempted to send 2 dates as parameters in the GET request, but I am only receiving this: Model public class Dates { public DateTime from { get; set; } public DateTime to { get; set; } } .net core [HttpGet] [Route("main/exportToEx ...

When viewing a React data table in Chromium browsers, the columns on the right side may flicker when the screen is small or the browser

I recently integrated the React data grid Npm package by adazzle. You can find more information about it here. I encountered an issue which you can see in this example: https://codesandbox.io/s/react-data-grid-example-9sb93?file=/src/App.tsx When using a ...

Is there a specific reason why the app.module.ts file is not generated when I initialize a new Angular application?

Each time I attempt to create a fresh Angular app with the ng new "your-app-name" command, the project structure does not include the app.module.ts file and there is no prompt from the Angular CLI regarding whether to add routing. Additionally, in the main ...

Prevent Angular from performing change detection on specific @Input properties

Within my Angular component, there are extensive calculations that take place when changes are detected. @Component ( selector: 'my-table', ... 400+ lines of angular template ... ) class MyTable implements OnDestroy, AfterContentInit, On ...

Avoid stopping Bootstrap Vue's events

Need help with a b-form-select control in Bootstrap Vue. Trying to execute a function when the value changes, but want the option to cancel the event and keep the original value. Complicating matters, the select is in a child component while the function ...

Error: The property referenced in the Typescript Ref of another Ref does not exist on the specified type

Utilizing the library https://github.com/lhz516/react-h5-audio-player, I am facing a challenge in correctly defining the type for my useRef without resorting to useRef<any>(null). I have managed to access and modify the audio element using the code ...

Using TypeScript to include a custom request header in an Express application

I am attempting to include a unique header in my request, but I need to make adjustments within the interface for this task. The original Request interface makes reference to IncomingHttpHeaders. Therefore, my objective is to expand this interface by intr ...

The installation of Node on Ubuntu 18.04 encountered an error

Could someone assist me with this problem? I initially had node installed, then uninstalled it using the rm -rf command following online suggestions. Now I am trying to reinstall it using nvm install node However, I'm encountering the following error ...

Sinon Stub generates varying values with each invocation

I'm pretty new to TypeScript and JavaScript, but I've managed to create a functioning VScode extension that I'm really happy with. However, I'm running into some issues with my Mocha tests. Here's a snippet of the code I'm str ...

Tips for resolving the 'JSX is not defined no-undef' error post TypeScript 4.4.2 update

Upon upgrading to TypeScript 4.4.2 from TypeScript 3.8.2, I have encountered numerous errors such as error 'x' is not defined no-undef. For instance, one of the errors is error 'JSX' is not defined no-undef. Upon closer inspection, most ...

Trying to automatically select a checkbox upon page load in Angular 6

When the page loads, I want to automatically check a checkbox. Here is my component: var user = ViewprojectComponent.featuresList1; this.rules_id = user[0]; for(let i = 0; i <= this.rules_id.length; i++) { var checkedOn1 = this.rules_id[i]; this.Ru ...

Upgrading from Angular 6 to Angular 7

After upgrading my Angular 4 app to Angular 6, I'm now looking to make the jump to Angular 7. According to a recent article, running the command below should only take around 10 minutes for the upgrade process: ng update @angular/cli @angular/core F ...

Tips for effectively simulating the formik useFormikContext function while writing unit tests using jest

I've created a simple component (shown below) that aims to fetch data from the Formik FormContext using the useFormikContext hook. However, I'm facing some challenges when writing unit tests for this component. It requires me to mock the hook, w ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

The Angular Button's tooltip is hidden behind the modal

Currently, I am employing Angular 13 alongside Bootstrap 5.0 and Mdb. One issue I encountered involves the modal I constructed using Mdb, which appears to be blocking the tooltip of buttons inside the modal. This causes the tooltips to not display correct ...

Constantly receiving an undefined value when trying to access a variable in an ngrx Observable

Here is the code snippet I am working with: language.component.ts form: FormGroup; first: AbstractControl; second: AbstractControl; constructor(private _fb: FormBuilder) { this.first = new FormControl('', [Validators.required, Va ...

Created computed getter and setter for Vue models (also known as "props") using the syntax of Vue Class Component

After exploring the limitations of v-model in Vue 2.x in this Stack Overflow thread, I discovered a way to connect parent and child components using v-model. The solution proposed is as follows: --- ParentTemplate: <Child v-model="formData"> ...

Creating Dynamic CSS Styling with Vendor Prefixes in Angular 2

I am exploring how to create a component in Angular 2 with two input arguments: colorOne and colorTwo. The goal is to fill a <div> with a gradient of these two colors. If I simply wanted to set the div background color to colorOne, I could achieve t ...

How can I determine if any of the values in the array (both previous and current) are identical?

I am facing a challenge with 3 input fields that could potentially have the same values, but I need to ensure uniqueness for each field. {productFormData.Roles.map((role: string, index: number) => { return ( <div className={`form-group in ...