Angularjs ng-model没有在我的mathjax-bind上工作

时间:2016-12-08 19:43:30

标签: javascript angularjs mathjax

我在字符串中输入了文本,如果找到##<input type="text" />之类的任何字符,则需要进行转换。如果我使用ng-bind-html,则返回值没有问题。但是,使用ng-model未返回mathjax-bind值。

如何解决这个问题?感谢。

HTML code:

<script type="text/x-mathjax-config"> MathJax.Hub.Config({   tex2jax: {inlineMath: [['`','`'], ['\\(','\\)']]} }); </script>

<div ng-controller="MyCtrl">
\(-3\times 4=\)

<br/>
<br/>
<br/>

  <div mathjax-bind="output">
  </div>
  <br/>
  <button ng-click="check_answer()">
  Check Answer
  </button>
</div>

控制器:

var myApp = angular.module('myApp',[]);

myApp.directive("mathjaxBind", function() {
        return {
            restrict: "A",
            controller: ["$scope", "$element", "$attrs",
                function($scope, $element, $attrs) {
                    $scope.$watch($attrs.mathjaxBind, function(texExpression) {
                        $element.html(texExpression);
                        MathJax.Hub.Queue(["Typeset", MathJax.Hub, $element[0]]);
                    });
                }]
        };
    });

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
     $scope.form = {};
    var output = 'Answer: <br/> ##';
    $scope.output = output.replace('##','<input type="text" ng-model="form.data.input1" />');

    $scope.check_answer = function()
    {
        alert($scope.form.data.input1);
    }
}

Mathjax-bind with angularjs model fiddle

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。我只需要再次编译......代码:

myApp.directive("mathjaxBind", function($compile) {
        return {
            restrict: "A",
            controller: ["$scope", "$element", "$attrs",
                function($scope, $element, $attrs) {
                    $scope.$watch($attrs.mathjaxBind, function(texExpression) {
                        $element.html(texExpression);
                        $compile($element.html(texExpression).contents())($scope);
                        MathJax.Hub.Queue(["Typeset", MathJax.Hub, $element[0]]);
                    });
                }]
        };
    });

这里是working fiddle