我建议使用由属性触发的指令。所以你的 HTML 看起来像:
<span eqn-bind="$F = ma$"></label>
和你的指令:
.directive('eqnBind', function () {
return {
restrict: "A",
controller: ["$scope", "$element", "$attrs", function ($scope, $element, $attrs) {
// Use this if it's a one-time thing and you don't need to re-render equations once they've been
// inserted into the DOM.
var value = $scope.$eval($attrs.esduEqnBind);
$element.html(value);
MathJax.Hub.Queue(["Reprocess", MathJax.Hub, $element[0]]);
/*
// Use this if you need to re-render eqns that already exist on the page or are going to need constant updates
$scope.$watch($attrs.eqnBind, function (value) {
$element.html(value);
MathJax.Hub.Queue(["Reprocess", MathJax.Hub, $element[0]]);
});
*/
}]
};
})
指令的第二部分(已注释掉)与来自“Getting MathJax to update after changes to AngularJS model”的answer 或多或少相同。
第一部分不需要$watch,所以效率应该更高。
顺便说一句,我发现了katex,它看起来更轻、更快。使用 katex 上面的变成:
.directive('eqnBind', function () {
return {
restrict: "A",
controller: ["$scope", "$element", "$attrs", function ($scope, $element, $attrs) {
// Use this if it's a one-time thing and you don't need to re-render equations once they've been
// inserted into the DOM.
var value = $scope.$eval($attrs.eqnBind);
$element.html(value);
renderMathInElement($element[0], { delimiters: [{ left: "$", right: "$", display: false }] });
/*
// Use this if you need to re-render eqns that already exist on the page...
$scope.$watch($attrs.eqnBind, function (value) {
$element.html(value);
renderMathInElement($element[0], {delimiters:[{ left: "$", right: "$", display: false }]});
});
*/
}]
};
})