简体   繁体   中英

Use MathJax in ngFor Angular (2/4/5)

So, I've been trying to render mathematical equations on Angular 4 application using MathJax. Everything is working fine till I had to render single equation. But now I need to render the around 4 equations which is coming from an API.

Now, using the MathJax inside ngFor works only for the first object in the array but not after that. See screenshot below:

在此处输入图片说明

My HTML looks like this:

<div class="card">
    <div class="card-header">
        //question HTML
    </div>
    <div *ngFor="let answer of model.question?.options">
        <span class="answers" #answers>{{answer?.text}}</span>
    </div>
</div>

And, the component looks like this:

@ViewChild('equation') equation: ElementRef;

MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.equation.nativeElement]);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.answers.nativeElement]);

Try:

<div class="card">
    <div class="card-header">
        //question HTML
    </div>
    <div *ngFor="let answer of model.question?.options">
        <span class="answers">{{answer?.text}}</span>
    </div>
</div>

@ViewChild('equation') equation: ElementRef;

MathJax.Hub.Queue(["Typeset", MathJax.Hub, this.equation.nativeElement]);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.getElementsByClassName("answers")]);

'# sintaxis is unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM