【问题标题】:AngularJS + highlight.js (to evaluate string with expression in directive)AngularJS + highlight.js(用指令中的表达式评估字符串)
【发布时间】:2012-11-16 05:14:22
【问题描述】:

我正在尝试创建 highlight.js 指令,但在应用范围变量时遇到问题。

<script src="http://code.jquery.com/jquery-1.8.2.min.js" ></script>
<link rel="stylesheet" href="http://yandex.st/highlightjs/7.3/styles/default.min.css">
<script src="http://yandex.st/highlightjs/7.3/highlight.min.js"></script>

<div ng-app="app">
    <div ng-controller="MyCtrl">
        <snippet>&lt;script src=&quot;{{src}}&quot;&gt;&lt;/script&gt;</snippet>
        {{src}}
    </div>
</div>

​

function MyCtrl($scope) {
  $scope.src = "foo.js";   
}

app.directive('snippet', ['$timeout', function($timeout) {
    var template = '<pre><code></code></pre>';

    return {
        restrict: 'E',
        compile: function(tElement, tAttrs, transclude) {

            var rawCode = tElement.text();
            tElement.html(template);

            return function(scope, element, attrs) {
                $timeout(function() {
                    scope.$apply(function() {
                        var formattedCode = hljs.highlightAuto(rawCode);
                        $(element).find('code').html(formattedCode.value);
                    });
                }, 0);
            }
        }
    }
}]);​

这里是小提琴:http://jsfiddle.net/dkrotts/RE7Jj/5/

如你所见,$scope.src 并没有在 sn-p 中应用它的值。我做错了什么?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    关键是你应该使用$interpolate 而不是$compile

    $interpolate 的说明

    将带有标记的字符串编译为插值函数。这个 服务由 HTML $compile 服务用于数据绑定。看 $interpolateProvider 用于配置插值标记。

    当你使用 $complie 时,它​​会将你的字符串变成元素。

    $compile 说明

    将一段 HTML 字符串或 DOM 编译成模板并生成一个 模板函数,然后可用于链接范围和 模板在一起。

    (说实话,在尝试之前我并没有真正理解描述。)

    这是工作的plunk

    app.controller('MainCtrl', function($scope) {
      $scope.cdnPath = "//path/to/cdn/";
      $scope.version = "1.0"; 
    });
    
    app.directive('snippet', ['$timeout', '$interpolate', function($timeout, $interpolate) {
        return {
            restrict: 'E',
            template:'<pre><code ng-transclude></code></pre>',
            replace:true,
            transclude:true,
            link:function(scope, elm, attrs){             
                var tmp =  $interpolate(elm.find('code').text())(scope);
                 $timeout(function() {                
                    elm.find('code').html(hljs.highlightAuto(tmp).value);
                  }, 0);
            }
        };
    }]);
    

    【讨论】:

    • 比我的回答更干净。我承认!
    • 我正在寻找带有此代码的&lt;p&gt;&lt;snippet&gt;&lt;/snippet&gt;&lt;/p&gt;,我最终得到&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;,而不是&lt;p&gt;标签内剩余的sn-p。有什么想法吗?
    【解决方案2】:

    您需要$compile 内部 HTML。请参阅下面的小提琴。您也不需要在 $apply 块中运行。

    app.directive('snippet', ['$timeout', '$compile', function($timeout, $compile) {
        var template = '<pre><code></code></pre>';
    
        return {
            restrict: 'E',
            compile: function(tElement, tAttrs, transclude) {
    
                var rawCode = tElement.text();
                tElement.html(template);
    
                return function(scope, element, attrs) {
    
                    var g = $compile(rawCode)(scope);
    
                    $timeout(function() {
                        var text = g[0].outerHTML;
                            var formattedCode = hljs.highlightAuto(text);
                            $(element).find('code').html(formattedCode.value);
                    }, 0);
                }
            }
        }
        }]);​
    

    http://jsfiddle.net/roytruelove/jMC9n/1/

    【讨论】:

    • BTW - 这行得通,但我有一种使用优先级和嵌入的更优雅的解决方案的感觉。有人吗?
    • 谢谢!它确实有效,直到我在 标记中放入更复杂的内容。这是一个更新的小提琴:jsfiddle.net/dkrotts/jMC9n/3
    • 这更像是一个 jquery 问题,查看stackoverflow.com/questions/247023/…
    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 2012-05-29
    • 2012-09-26
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多