我是新手。我正在创建表单,提交表单后,我将获得对象中的数据。我需要将该对象传递给其他组件,是否可以使用@Input属性。如果是,我们如何使用@Input属性来实现。请帮助我。
parent.component.html
<form [formGroup]="addForm" (ngSubmit)="onSubmit()">
Name: <input type="text" formControlName="name" placeholder="Name"><br/>
Fname: <input type="text" formControlName="fname" placeholder="FirstNAme"><br/>
Lname: <input type="text" formControlName="lname" placeholder="LastName"><br/>
<button>Submit</button>
</form>
<h1>Hello {{message}}</h1> <br/>
<app-forms [parentObject]="FormObject">]</app-forms>
parent.component.ts
import { Component, OnInit,Input } from '@angular/core';
import { FormGroup ,FormBuilder } from '@angular/forms';
import {Router} from "@angular/router";
@Component({
selector: 'app-form1',
templateUrl: './form1.component.html',
styleUrls: ['./form1.component.css']
})
export class Form1Component implements OnInit {
addForm:FormGroup;
parentObject:Object;
constructor(private fb:FormBuilder, private router: Router) { }
ngOnInit() {
this.addForm=this.fb.group({
name:[''],
fname:[''],
lname:['']
})
}
onSubmit(){
console.log(this.addForm.value);
var FormObject=this.addForm.value
}
}
child.component.ts
import { Component, OnInit,Input } from '@angular/core';
@Component({
selector: 'app-forms',
templateUrl: './forms.component.html',
styleUrls: ['./forms.component.css']
})
export class FormsComponent implements OnInit {
@Input() parentObject: Object;
constructor() { }
ngOnInit() {
console.log(this.parentObject);
}
}
答案 0 :(得分:0)
在您的子组件中将输入定义为:
import { Input} from '@angular/core';
@Input() some_variable_name: Object;
现在在父组件中将对象传递为:
<child-component [some_variable_name] = "object_variable" />
查看官方文档以了解更多详细信息:https://angular.io/guide/component-interaction