-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngJsonHtml.js
38 lines (36 loc) · 1.08 KB
/
ngJsonHtml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
angular.module('ng-json-html', [])
.directive('dynamic', function ($compile) {
var attrRender = function(data){
var rtn = data.split('|').map(function(item){
if( item.indexOf('=') == -1 )
return item.replace(/\.\[/, 'class="').replace(/\#\[/,'id="').replace(/\@\[/,'href="').replace(/\]/,'"').replace(/\|/g,' ');
else
return item.replace(/\|/g,' ');
})
return rtn.join(' ');
}
var convert = function(json){
var html = '';
angular.forEach(json, function(item, index)
{
html += "<"+attrRender(index)+">";
if( typeof item == "string" ) html += item;
if( Array.isArray(item) ) html += item.reduce(function(prev, val){
return prev + convert(val);
}, '');
if( item instanceof Object && !Array.isArray(item) ) html += convert( item );
html += "</"+index.split('|')[0]+">";
})
return html;
}
return {
restrict: 'A',
replace: true,
link: function (scope, ele, attrs) {
scope.$watchCollection( attrs.dynamic, function(data) {
ele.html( convert(data) );
$compile(ele.contents())(scope);
});
}
};
});