I'm trying to prettify some text that is generated dynamically.
<div ng-app="Knob" ng-controller="myCtrl">
<pre class="prettyprint">{{text}}</pre>
</div>
var App = angular.module('Knob', []);
App.controller('myCtrl', function($scope) {
$scope.text = "hello world";
})
App.directive('prettyprint', function() {
return {
restrict: 'C',
link: function postLink(scope, element, attrs) {
prettyPrint();
}
};
});
The output:
hello worldtext}}
Any ideas why?
Hard to say, what does prettyPrint() should return ?
It seems quite strange that you don't give any arguments to prettyPrint...
By the way, I think an angular filter would be a better fit for your need instead of a directive.
[EDIT]
This one is working "dynamically" using filter :
html :
js :
You can see it at
http://jsfiddle.net/cSXpV/1/
You can uncomment the input to change directly the text that will be prettyfied
Note that this version is for Angular 1.1.1 (the version you choosed in your initial jsfiddle), for Angular 1.2.*, you have to use ng-bind-html and the ngSanitize module
Last point : now that it is dynamically prettyfied, the setTimeOut and $scope.$apply can be removed (info for readers)
[/EDIT]