Skip to content

Instantly share code, notes, and snippets.

@pc035860
Created August 6, 2013 15:05
Show Gist options
  • Save pc035860/6165303 to your computer and use it in GitHub Desktop.
Save pc035860/6165303 to your computer and use it in GitHub Desktop.
A directive highlights a served object with JSON format with angular-highlightjs.
angular.module('myApp')
.directive('hljsToJson', [function () {
return {
restrict: 'EA',
scope: {
obj: '=hljsToJson'
},
template: '<div hljs source="prettyJSON"></div>',
link: function postLink(scope, iElm, iAttrs) {
var tabWidth = 4;
scope.prettyJSON = '';
scope.$watch('obj', function (newObj, oldObj) {
if (angular.isDefined(newObj) &&
(newObj === oldObj || !angular.equals(newObj, oldObj))) {
scope.prettyJSON = JSON.stringify(newObj, null, tabWidth);
}
});
}
};
}]);
@pc035860
Copy link
Author

pc035860 commented Aug 6, 2013

Demo

Requirements

  • AngularJS v1.0.1+
  • Highlight.js v7.3+
  • angular-highlightjs v0.0.1+

Usage

<div hljs-to-json="myObject"></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment