在* ngIf指令中使用ViewChild时,我将Button设为未定义。但是当我检查日志时,它显示如下内容
https://i.stack.imgur.com/mpb16.png
就我而言,它仍然无法正常工作。有人可以帮忙吗?
HTML代码
<div *ngIf="someCondition()">
<div class="row top-container">
<div class="col-md-6">
<h1 class="text-center">Registered </h1>
</div>
<div class="col-md-6">
<div class="pull-right header-buttons-container">
<button #registerPartnerButton id="registerPartner" class="btn btn-primary" type="button" (click)="registerUser()">Register</button>
</div>
</div>
</div>
</div>
TS代码
registerButton: ElementRef;
@ViewChildren('registerPartnerButton') set someDummyName(content: ElementRef) {
this.registerPartnerButton = content;
};
constructor(changeDetector: ChangeDetectorRef) {}
问题: 应用上述解决方案后,我得到的registerPartnerButton为未定义。但是当我检查日志时,它显示如下内容
https://i.stack.imgur.com/mpb16.png
我希望ViewChild一定不要不确定。
答案 0 :(得分:0)
Finally I fixed it by applying below solution:
registerButton: ElementRef;
@ViewChildren('registerPartnerButton') set setButton(content: QueryList<ElementRef>)
{
if(content.length > 0) {
this.registerButton = content.first;
// do further actions
}
};