Javascript 如何将getElementById与*ngIf一起使用?
我正在尝试选择一个带有*ngIf的div:Javascript 如何将getElementById与*ngIf一起使用?,javascript,angular,dom,viewchild,Javascript,Angular,Dom,Viewchild,我正在尝试选择一个带有*ngIf的div: <div id="explanation" *ngIf="isCorrect(option.optionValue)"> 使用getElementById时返回null。如果我删除ngIf,它可以正常工作,但我需要在我的应用程序中使用ngIf。那么我如何选择div呢 编辑:重构代码如下: <div [hidden]="!isCorrect(option.optionValue)"> <div id="explan
<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);
}
选项{question.answer}}是正确的,因为{{question.explainion}。
ngAfterViewInit(){
让itemFrom=document.getElementById('解释');
让itemTo=document.getElementById('question');
}
无线电变化(回答){
this.question.selectedOption=答案;
this.answer.emit(应答);
this.moveexplainion(this.itemFrom,this.itemTo);
}
移动说明(从,到){
to.替换为(from);
}
问题和解释现在都已记录,替换为works,只需从正确答案下面删除原始解释,每个问题的解释应移到问题框中,并同样从正确答案下面删除解释。如果您想使用TypeScript访问您的div,然后你需要用一个标签来标记它
<div id="explanation" *ngIf="isCorrect(explanation)" #explanation>
并将名称添加到您使用的参数中,以传递值
或与
<div [(ngModel)]="option"></div
如果您想使用TypeScript访问div,则需要使用Hashtag标记它
<div id="explanation" *ngIf="isCorrect(explanation)" #explanation>
并将名称添加到您使用的参数中,以传递值
或与
<div [(ngModel)]="option"></div
你没有。您使用了一个I'm use@ViewChild('explaution',{static:true})私有解释:ElementRef;但是在登录ngAfterViewInit时出现未定义的错误您的条件是否得到了尊重?在上面添加了ngAfterViewInit代码,解释和问题都被记录了下来,但是replaceWith不起作用。现在需要删除正确答案下面的原始解释元素。还需要它来处理以下问题-每个问题的解释都应该显示在问题框中。你不需要。您使用了一个I'm use@ViewChild('explaution',{static:true})私有解释:ElementRef;但是在登录ngAfterViewInit时出现未定义的错误您的条件是否得到了尊重?在上面添加了ngAfterViewInit代码,解释和问题都被记录了下来,但是replaceWith不起作用。现在需要删除正确答案下面的原始解释元素。还需要它来处理以下问题-每个问题的解释都应该显示在问题框中;这是使用ViewChild的正确方法吗?我需要把解释部分移到问题部分。正在尝试使用此代码,但JS似乎不起作用:moveExpression(){//let itemFrom=document.getElementById('expression');let itemTo=document.getElementById('question');//console.log(itemFrom);console.log(itemTo);const moveItems=函数(from,to){to.replacetwith(from);};moveItems(this.explainion,itemTo);我在ngAfterViewInit中做了console.log(this.explainion),它说它是未定义的。我认为您可以直接使用*ngIf!比如*ngIf=“option==true”。确保在x.component.tsI中切换选项(true/false),添加了hashtag,现在尝试使用@ViewChild('explainion',{static:false})解释:ElementRef;这是使用ViewChild的正确方法吗?我需要将解释div移到问题div所在的位置。尝试使用此代码,但JS似乎不起作用:moveExplation(){//let itemFrom=document.getElementById('explaution');let itemTo=document.getElementById('question');//console.log(itemFrom);console.log(itemTo);const moveItems=function(from,to){to.replaceWith(from);};moveItems(this.expression,itemTo);我在ngAfterViewInit中创建了console.log(this.expression),它说它未定义。我认为您可以直接使用*ngIf!比如*ngIf=“option==true”。请确保切换该选项(对/错)在您的x.component.ts中