I have a child component in which I have rendered the text form input controls
. My submit button is rendered in parent component so while submitting the form
I am getting the empty data
.
parent-component.html
<form #f="ngForm" name="myForm">
<app-text [users]="users"></app-text>
<br><br>
<button (click)="save(f)">Save</button>
</form>
parent.component.ts
import { Component } from '@angular/core';
import {
NgForm
} from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class ParentComponent {
public users = {
'firstname' :'',
'lastname' :''
};
public constructor()
{
}
save(myForm:NgForm)
{
console.log(myForm.value);
}
}
child.component.ts (TextComponent)
import { Component,Input } from '@angular/core';
@Component({
selector: 'app-text',
templateUrl: './text.component.html',
styleUrls: [ './text.component.css' ]
})
export class TextComponent {
@Input() users:any;
}
child.componet.html
<label>Enter the First Name</label>
<input type="text" [(ngModel)]="users.firstname" name="firstname">
<label>Enter the Last Name</label>
<input type="text" [(ngModel)]="users.lastname" name="lastname">
app.module.ts
import { NgModule,NO_ERRORS_SCHEMA,CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TextComponent } from './text/text.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, TextComponent],
bootstrap: [ AppComponent ],
schemas:[CUSTOM_ELEMENTS_SCHEMA,NO_ERRORS_SCHEMA]
})
export class AppModule { }
Here I am submitting my form with values and trying to fetch the data into save method of parent component but I am getting the empty data.
How can I achieve it? is there any other way to achieve it?
Please help me. Thanks.