[英]Reactive Form Parent component pass in data to Child component
This is my parent component. 这是我的父组件。 When i click on a row in this ag-grid-angular table, it will pass in the selected row details to the child component "app-book-detail-reactive-form".
当我单击此ag-grid-angular表中的一行时,它将把选定的行详细信息传递给子组件“ app-book-detail-reactive-form”。
<div class="jumbotron">
<div [hidden]="success">
<h1 class="display-4">Welcome!</h1>
<p class="lead">Manage your books here (Add, Edit etc)</p>
<button type="button" class="btn btn-primary" style="float: right;" (click)="goAddBook()"><span class="fas fa-plus-circle"></span> Add Book</button><br>
<hr class="my-4">
</div>
<ag-grid-angular #agGrid style="width: 100%; height: 350px;"
class="ag-bootstrap"
[gridOptions]="gridOptions"
[showToolPanel]="showToolPanel"
rowSelection="single"
(selectionChanged)="onSelectionChanged($event)"
(gridReady)="onGridReady($event)">
</ag-grid-angular>
<app-book-detail-reactive-form [book]="selectedBook"></app-book-detail-reactive-form>
</div>
This is my child component "app-book-detail-reactive-form" which will accept the selectedBook values from parent component. 这是我的子组件“ app-book-detail-reactive-form”,它将接受父组件中的selectedBook值。 The values sent are in an array of the form
发送的值采用以下形式的数组
[{
title: "harry potter",
description: "some cool stuff",
instance_list: [{...}, {...}]
}]
/// ///
export class BookDetailReactiveFormComponent implements OnInit {
@Input() book: any[] = [{
title: '',
description: '',
instance_list: []
}];
constructor(
private modalService: NgbModal,
private fb: FormBuilder
) {}
ngOnInit() {
console.log(this.book);
this.bookDetailForm = this.fb.group(
{
title: [this.book[0].title, Validators.required],
description: [this.book[0].description],
instance_list: this.fb.array(this.book[0].instance_list)
}
)
}
}
This is my html for my reactive form of child component 这是我的子组件的反应形式的HTML
<div *ngIf="book" [hidden]="success">
<form [formGroup]='bookDetailForm' (ngSubmit)="onSubmit()" [hidden]="success">
<div class="form-group">
<div class="row">
<div class="col-sm-9">
<div class="form-group">
<label>Title of the Book</label>
<input type="text" class="form-control" placeholder="Title of Book" formControlName="title">
<div class="alert alert-danger" role="alert" *ngIf="titleAlert.invalid && titleAlert.touched">
Title of Book required!
</div>
</div>
<div class="form-group">
<label>Description of Book</label>
<textarea class="form-control" rows="3" placeholder="Enter Description of book..." formControlName="description"></textarea>
</div>
</div>
</div>
<hr class="my-4">
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
I just want to pass the values selected from parent component to child componenet and set those selected values as the @input book value in my child component when it is available. 我只想将从父组件中选择的值传递给子组件,并在可用时将这些所选值设置为子组件中的@input图书值。 Of course the values of @input book is empty at the start.
当然,@ input book的值在开始时为空。 I tried to initialized it to some values but i still get the following error.
我试图将其初始化为某些值,但仍然收到以下错误。
I am getting the error "AllBooksComponent.html:15 ERROR TypeError: Cannot read property '0' of undefined". 我收到错误“ AllBooksComponent.html:15错误TypeError:无法读取未定义的属性'0'”。
How can i solve this? 我该如何解决?
Perhaps a simple: 也许很简单:
if(book !== null){
this.bookDetailForm = this.fb.group(
{
title: [this.book[0].title, Validators.required],
description: [this.book[0].description],
instance_list: this.fb.array(this.book[0].instance_list)
}
)
} }
Could be enought? 够了吗?
Not use ngInit, use a setter in input, see https://angular.io/guide/component-interaction#intercept-input-property-changes-with-a-setter 不使用ngInit,在输入中使用setter,请参阅https://angular.io/guide/component-interaction#intercept-input-property-changes-with-a-setter
_book:any; //declare a variable _book
@Input()
set book(book:any){
this._book=book;
this.bookDetailForm = this.fb.group(
{
title: [book.title, Validators.required],
description: [book.description],
instance_list: this.fb.array(book.instance_list)
}
)
}
get book()
{
return this._book;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.