【发布时间】:2016-07-22 02:47:00
【问题描述】:
有没有人让 Mathjax 与 Angular2 一起工作?
Plunkr 示例已创建:- http://plnkr.co/edit/FLduwmtHfkCN5XPfzMsA?p=preview
从一些 Angular1 示例中,我看到调用 MathJax.Hub.Queue 似乎需要一个指令,但我怀疑我需要很长时间才能正确使用 Angular 2 语法,所以我想知道是否有人已经这样做了吗?
例如,以下是 Angular 1 示例。 https://github.com/ColCarroll/ngMathJax/blob/master/ng-mathjax.js
mathjax 语法在这里:- https://docs.mathjax.org/en/v1.1-latest/typeset.html
尝试在 Angular2 中做类似的事情。
更新 - 感谢 Thierry,以下工作正常。
组件:-
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++;
}
}
指令:-
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]);
}
}
仍然不知道为什么我需要在两个地方都排队重新渲染。
【问题讨论】:
-
上面的 plunkr 现在正确初始化并且 Angular 正确显示了 MathJax,但在数据更新上仍然不能正常工作(点击 plunkr 按钮查看)。有什么想法吗?
标签: angular mathjax angular2-directives