Angular 2-在DIV上,将对象传递到另一个组件,而另一个组件使用该对象调用REST服务



在parent(parentcomponent(中加载所有父数据onInit方法。现在,如果我单击单个父母,它会通过将父级从parentcomponent传递到parentdetailcomponent和parentDetailComponent调用parentdetailservice并获取iParent对象来触发孩子的视图。现在,我尝试使用 *ngif显示ParentDetailComponent模板,但并未触发。我应该如何触发儿童视图?

儿童模板:

<div *ngIf="DetailIsVisible" id="detailModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
            <h4 class="modal-title" id="myModalLabel">Parent Detail</h4>
        </div>
        <div class="modal-body">
            <div *ngIf="parent">
                <h2>{{parent.name}} details!</h2>
                <div><label>id: </label>{{parent.Id}}</div>
                <div>
                    <label>name: </label>
                    <label>{{parent.name}}</label>
                </div>
                <div>
                    <label>Education: </label>
                    <label>{{parent.education}}</label>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" (click)="hideDetail()">Close</button>
        </div>
    </div>
</div>

儿童组件:

@Component({
selector: 'app-parent-detail',
templateUrl: './child.component.html',
providers: [ParentDetailService]
})
export class ParentDetailComponent{
parentId: string;
parent: IParent;
public DetailIsVisible: boolean;
constructor(private _parentDetailService: ParentDetailService) {
}
show(parentId: string) {
    this.serviceDetail = serviceId;
 this._parentDetailService.getParentServiceDetail(this.parentId).subscribe(parent => {
        this.parent = parent[0];
        console.log(this.parent);
    });
}
hideDetail() {
    this.DetailIsVisible = false;
}

}

父模板:

div class="container">
<div class="row">
    <div class="col-sm-4" *ngFor="let parent of parents" (click)="selectedServiceDetail(parent.Id)" data-target="#detailModal">
        <div class="panel panel-primary" >
            <div class="panel-heading">{{parent.Id}}</div>
            <div class="panel-body"><img [src]="parent.thumbnailpath"
                                         class="img-responsive" style="width:100%; height: 200px" [title] = 'parent.name' alt="Image"></div>
            <div class="panel-footer">{{parent.name}}</div>
        </div>
    </div>
</div>


父组件:

 @Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
providers: [ParentDetailService]
 })
 export class ParentComponent implements OnInit {
/**all rest api and assign to array of catalog*/
     public parents: IParent[];
     public DetailIsVisible: boolean;
     @ViewChild(ParentDetailComponent) detailModal: ParentDetailComponent;
constructor(private _parentService: ParentService) {
}
ngOnInit(): void {
    this._parentService.getParents()
        .subscribe(
            parents => this.parents = parents
        );
    this.detailModal.DetailIsVisible = false;
}
    public selectedServiceDetail(parentId): void {
    this.detailModal.DetailIsVisible = true;
    this.detailModal.show(parentId); ---> this triggers ChildComponent method to fetch parent detail service
}

您可以使用mddialog。

 openNewDetailModal(productObj: any): void {
            const dialogRef = this.dialog.open(DetailsComponent);
            dialogRef.componentInstance.album = this.selectedAlbumObj;
            dialogRef.afterClosed().subscribe(
                (result: boolean) => {
                   // do anything after close modal
                });
}

还请在此处查看更多详细信息,请单击此帮助!

您必须使用此@input(在您的详细信息组件中(使用事件发射器,该值将从产品页面传递。这将把产品链接到其详细信息页面,然后详细信息页面将获取输入,并在其ngoninit上调用将加载详细信息的服务方法。

所有这些都必须在模态i:e中使用角材料(如果需要(,这将解决您的问题。

https://material.angular.io/components/component/dialog

如果您有plunker或为此问题尝试了一些事情,请更新。

上面的解决方案对我有用。问题是CSS。我创建了自己的CSS课程,以获取弹出式感觉

相关内容

  • 没有找到相关文章

最新更新