Has anyone gotten Mathjax working with Angular2 ?
Plunkr example created :- http://plnkr.co/edit/FLduwmtHfkCN5XPfzMsA?p=preview
From some Angular1 examples I saw it looks like a directive is needed to call MathJax.Hub.Queue, but I suspect it will take me quite a while to get the Angular 2 syntax right, so I wondered if anyone has already done it ?
e.g the following is an Angular 1 example. https://github.com/ColCarroll/ngMathJax/blob/master/ng-mathjax.js
And the mathjax syntax is here :- https://docs.mathjax.org/en/v1.1-latest/typeset.html
Trying to do something similar in Angular2.
UPDATE - the following works, thanks to Thierry.
Component:-
import {Component, OnInit} from 'angular2/core';
import {MathJaxDirective} from './mathjax.directive';
@Component({
selector: 'hello-mathjax',
templateUrl: 'app/hello_mathjax.html',
directives: [MathJaxDirective]
})
export class HelloMathjax {
fractionString: string = 'Inside Angular one half = $\\frac 12$';
index: number = 3;
ngOnInit() {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"MathJax"]);
}
update () {
this.fractionString = 'Inside Angular one third = $\\frac 1'+this.index+'$';
this.index++;
}
}
Directive:-
import {Directive, ElementRef, Input} from 'angular2/core';
@Directive({
selector: '[MathJax]'
})
export class MathJaxDirective {
@Input('MathJax') fractionString: string;
constructor(private el: ElementRef) {
}
ngOnChanges() {
console.log('>> ngOnChanges');
this.el.nativeElement.style.backgroundColor = 'yellow';
this.el.nativeElement.innerHTML = this.fractionString;
MathJax.Hub.Queue(["Typeset",MathJax.Hub, this.el.nativeElement]);
}
}
Still not sure why I need to queue the re-render in both places.