[英]How to append child component into parent component conditionally?
I have two components, parent and register component. 我有两个组件,父组件和寄存器组件。
ParentComponent.html looks like: ParentComponent.html看起来像:
<div class="tab-content">
<div class="tab-pane" id="signup" (click)="getChild()">
<register></register> --//register component
</div>
</div>
register.component.ts looks like: register.component.ts看起来像:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'register',
templateUrl: '../templates/register.html'
})
export class RegisterComponent implements OnInit {
ngOnInit() {
this.registerService.getRoleList()
.subscribe(res => {
if(res.data){
this.instRoleList = res.data.roleList;
}
});
/* ngOnInit calls a service from which data is received and certain
conditions are met. I want this service to go only when getDetails() is called and <register> should get append to <div id="signup>
*/
}
}
register.html code snippet looks like: ( it depends on the data received from register service data "") register.html代码段如下所示:(取决于从注册服务数据“”接收的数据)
<div class="form-group group" *ngIf="instRoleList">
</div>
So, I want that on click of getChild(), register component gets appended to <div id="signup">
因此,我希望单击getChild()时,将注册组件附加到<div id="signup">
How to achieve this? 如何实现呢?
<div class="tab-pane" id="signup" (click)="getChild()">
<!-- register component to append here on getChild() click -->
</div>
I have already used below code but didn't work. 我已经使用了下面的代码,但是没有用。
$('#signup').append('<register></register>')
This could easily be archieved with the ngIf directive: 这可以很容易地用ngIf指令归档:
<div class="tab-content">
<div class="tab-pane active" id="signin">
<!-- other code -->
</div>
<div class="tab-pane" id="signup" (click)="getChild()">
<register *ngIf="condition"></register> --//register component
</div>
</div>
@Component({
selector: 'register',
templateUrl: '../templates/register.html'
})
export class ParentComponent {
condition: boolean = false;
getChild(){
this.condition = !this.condition;
}
}
This click event will work as a toggle function with this approach. 此点击事件将通过这种方法用作切换功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.