【发布时间】:2020-09-02 06:37:00
【问题描述】:
这是我的子组件
子 html //add-child.component.html
<div class="content">
<h6 class="title" translate>
<span *ngIf="addTitle" translate>Add</span>
<span *ngIf="editTitle" translate>Edit</span>
</h6>
<div class="row">
<form [formGroup]="administrationForm">
<input type="text" class="form-control form-control-sm"
formControlName="adminId"
</form>
</div>
</div>
子组件...
@Component({
selector: 'app-add-hospital',
templateUrl: './add-child.component.html'
})
export class AddChildComponent implements OnInit {
addTitle = true;
editTitle = false;
}
父组件.html
<button (click)="enableChild()"></button>
<app-add-child #newChild></app-add-child>
父组件
@Component({
selector: 'app-hospitals',
templateUrl: './parent.component.html'
})
export class ParentComponent implements OnInit {
@ViewChild(AddChildComponent , { static: true }) newChild: AddChildComponent ;
@ViewChild(AddChildComponent , { static: true }) set Content(newChildRef) {
this.newChild = newChildRef;
}
constructor(){}
enableChild() {
// this.add = true;
this.newChild.addTitle = false;
this.newChild.editTitle = true;
this.loadById(event);
}
loadById() {
//server call
this.hospitalService.getById().subscribe(response => {
this.newChild.administrationForm.patchValue({
adminId: result["adminId"]
})
}
}
在加载父级时,它会加载子级。它会在父母页面的末尾导致大量空白空间。只有在调用 Enable child 方法时才需要加载子 DOM。我尝试在加载子项时在父项中添加 ngIf,但这会导致初始化问题,需要使用某些子项值。
如何在仅需要时加载子项。(ngIf 不工作,因为启用时我需要子项引用)
【问题讨论】:
-
请显示调用
loadHospitalById()的位置。显示loadById方法的实现。显示使用this.newChild的任何其他地方。更好的是,为父母和孩子发布整个模板和组件代码,因为我怀疑这是xy question
标签: angular angular6 angular7 angular8 viewchild