我正在尝试选择一个带有*ngIf的div:
<div id="explanation" *ngIf="isCorrect(option.optionValue)">当我使用getElementById时,它返回null。如果我删除了ngIf,它可以正常工作,但我需要在我的应用程序中使用ngIf。那么我该如何选择div呢?
编辑:重构后的代码如下:
<div [hidden]="!isCorrect(option.optionValue)">
<div id="explanation">
Option {{ question.answer }} is correct because {{ question.explanation }}.
</div>
</div>
ngAfterViewInit() {
let itemFrom = document.getElementById('explanation');
let itemTo = document.getElementById('question');
}
radioChange(answer) {
this.question.selectedOption = answer;
this.answer.emit(answer);
this.moveExplanation(this.itemFrom, this.itemTo);
}
moveExplanation(from, to) {
to.replaceWith(from);
}问题和解释现在都被记录下来了,replaceWith工作,只需要从正确答案下面删除原始解释,每个问题的解释应该移动到问题框中,类似地从正确答案下面删除解释。
发布于 2019-09-30 22:59:46
如果您想要使用TypeScript访问您的div,那么您需要用一个标签来标记它
<div id="explanation" *ngIf="isCorrect(explanation)" #explanation>并将名称添加到您使用的参数中,以传递该值!
或使用
<div [(ngModel)]="option"></divhttps://stackoverflow.com/questions/58169942
复制相似问题