我正在尝试创建 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><script src="{{src}}"></script></snippet>
{{src}}
</div>
</div>
</p>
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 没有在代码段中应用它的值。我究竟做错了什么?