2 An Angular JS plugin to compare and show object differences in JSON format. [Demo](http://hipster-labs.github.io/angular-object-diff/)
4 ![Screenshot](/screenshot.png)
9 bower install angular-object-diff --save
13 <link type="text/css" href="bower_components/dist/angular-object-diff.css" rel='stylesheet'>
14 <script type="text/javascript" src="bower_components/dist/angular-object-diff.js"></script>
19 npm i angular-object-diff
22 # Available methods on `ObjectDiff` service
25 `setOpenChar`: set the opening character for the view, default is `{`
27 `setCloseChar`: set the closing character for the view, default is `}`
29 `diff`: compare and build all the difference of two objects including prototype properties
31 `diffOwnProperties`: compare and build the difference of two objects taking only its own properties into account
33 `toJsonView`: format a diff object to a full JSON formatted object view
35 `toJsonDiffView`: format a diff object to a JSON formatted view with only changes
37 `objToJsonView`: format any javascript object to a JSON formatted view
42 `toJsonView`: format a diff object to a full JSON formatted object view
44 `toJsonDiffView`: format a diff object to a JSON formatted view with only changes
46 `objToJsonView`: format any javascript object to a JSON formatted view
51 Declare the dependency
53 angular.module('myModule', ['ds.objectDiff']);
60 angular.module('myModule')
61 .controller('MyController', ['$scope', 'ObjectDiff', function($scope, ObjectDiff){
62 $scope.yourObjectOne = {//all your object attributes and values here};
63 $scope.yourObjectTwo = {//all your object attributes and values here};
65 // This is required only if you want to show a JSON formatted view of your object without using a filter
66 $scope.yourObjectOneJsonView = ObjectDiff.objToJsonView($scope.yourObjectOne);
67 $scope.yourObjectTwoJsonView = ObjectDiff.objToJsonView($scope.yourObjectTwo);
69 // you can directly diff your objects js now or parse a Json to object and diff
70 var diff = ObjectDiff.diffOwnProperties($scope.yourObjectOne, $scope.yourObjectTwo);
72 // you can directly diff your objects including prototype properties and inherited properties using `diff` method
73 var diffAll = ObjectDiff.diff($scope.yourObjectOne, $scope.yourObjectTwo);
75 // gives a full object view with Diff highlighted
76 $scope.diffValue = ObjectDiff.toJsonView(diff);
78 // gives object view with onlys Diff highlighted
79 $scope.diffValueChanges = ObjectDiff.toJsonDiffView(diff);
84 Bind the variables directly in your html using the `ng-bind-html` angular directive.
85 Use a `<pre>` element for better results
88 <pre ng-bind-html="diffValue"></pre>
89 <pre ng-bind-html="diffValueChanges"></pre>
90 <pre ng-bind-html="yourObjectOneJsonView"></pre>
91 <pre ng-bind-html="yourObjectTwoJsonView"></pre>
94 The same can be done with filters as well
97 angular.module('myModule')
98 .controller('MyController', ['$scope', 'ObjectDiff', function($scope, ObjectDiff){
99 $scope.yourObjectOne = {//all your object attributes and values here};
100 $scope.yourObjectTwo = {//all your object attributes and values here};
102 // you can directly diff your objects js now or parse a Json to object and diff
103 var diff = ObjectDiff.diffOwnProperties($scope.yourObjectOne, $scope.yourObjectTwo);
105 // you can directly diff your objects including prototype properties and inherited properties using `diff` method
106 var diffAll = ObjectDiff.diff($scope.yourObjectOne, $scope.yourObjectTwo);
111 Bind the variables directly in your html using the `ng-bind-html` angular directive.
112 Use a `<pre>` element for better results
115 <pre ng-bind-html="diffValue | toJsonView"></pre>
116 <pre ng-bind-html="diffValueChanges | toJsonDiffView"></pre>
117 <pre ng-bind-html="yourObjectOneJsonView | objToJsonView"></pre>
118 <pre ng-bind-html="yourObjectTwoJsonView | objToJsonView"></pre>
121 Inspired from https://github.com/NV/objectDiff.js