简体   繁体   English

将HTML元素追加到现有元素的AngularJS指令

[英]AngularJS directive that appends HTML elements to existing element

I have an AngularJS , JS , JQ , HTML5 web app, which is capable of sending different HTTP methods to our project's RESTful Web Service and receiving responses in JSON . 我有一个AngularJSJSJQHTML5 Web应用程序,该应用程序能够向我们项目的RESTful Web Service发送不同的HTTP方法,并以JSON接收响应。

It looks like this: 看起来像这样:

在此处输入图片说明

What I want is to create an AngularJS directive , which could accept JSON object and create an <li> for every JSON property it finds. 我想要的是创建一个AngularJS directive ,该AngularJS directive可以接受JSON对象,并为找到的每个JSON属性创建一个<li> If property itself is an object - the function should be called recursively. 如果属性本身是对象,则应递归调用该函数。

Basically, I search a way to parse a JSON object to HTML elements in a such way that following JSON: 基本上,我搜索一种将JSON对象解析为HTML元素的方式,其方式如下JSON:

{
    "title": "1",
    "version": "1",
    "prop" : {
         "a" : "10",
         "b" : "20",
         "obj" : {
              "nestedObj" : {
                   "c" : "30"
               } 
          }
     }
}

Would be transfrormed into following html: 将转换为以下html:

<ul>
    <li>title : 1</li>
    <li>version : 1</li>
    <li>
        prop : 
        <ul>
            <li>a: 10</li>
            <li>b: 20</li>
            <li>
                obj : 
                <ul>
                    <li>
                        nestedObj : 
                        <ul>
                            <li>c : 30</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Does anyone know how to achieve this using AngularJS directives? 有谁知道如何使用AngularJS指令实现这一目标? Every useful answer is highly appreciated and evaluated. 每个有用的答案都受到高度赞赏和评估。

Thank you. 谢谢。

I tried this by recursivly include a directive. 我通过递归包含指令来尝试此操作。 But this seems be really ugly. 但这看起来真的很丑。

My solution is just like the plain old html generated out of a recursive method and append as element: 我的解决方案就像使用递归方法生成的普通旧html并追加为元素一样:

//recursivly generate the object output
scope.printObject = function (obj, content) {
    content = "<ul>";
    for (var i in obj) {
       if (angular.isObject(obj[i])) {
           content += "<li>"+i+""+scope.printObject(obj[i])+"</li>";
       } else {
           content += "<li>" + i + ":" + obj[i] + "</li>";
       }
    }
    content+="</ul>";
    return content;
 };

Full working code here: 完整的工作代码在这里:

http://jsfiddle.net/zh5Vf/1/ http://jsfiddle.net/zh5Vf/1/

It has little to do with Angular (it's plain old JS), but for the fun of it, here is a directive that does what you want: 它与Angular无关(它是普通的旧JS),但有趣的是,这里有一条指令可以满足您的要求:
(It is a bit more lengthy in order to properly format the HTML code (indent) and support custom initial indentation.) (为了正确格式化HTML代码(缩进)并支持自定义初始缩进,这要花一些时间。)

app.directive('ulFromJson', function () {
    var indentationStep = '    ';

    function createUL(ulData, indentation) {
        indentation = indentation || '';

        var tmpl = ['', '<ul>'].join('\n' + indentation);
        for (var key in ulData) {
            tmpl += createLI(key, ulData[key], indentation + indentationStep);
        }
        tmpl = [tmpl, '</ul>'].join('\n' + indentation);

        return tmpl;
    }

    function createLI(key, value, indentation) {
        indentation = indentation || '';

        var tmpl = '';
        if (angular.isObject(value)) {
            var newIndentation = indentation + indentationStep;
            tmpl += '\n' + indentation + '<li>' +
                    '\n' + newIndentation + key + ' : ' +
                    createUL(value, newIndentation) +
                    '\n' + indentation + '</li>';
        } else {
            tmpl += '\n' + indentation + '<li>' + key + 
                    ' : ' + value + '</li>';
        }

        return tmpl;
    }

    return {
        restrict: 'A',
        scope: {
            data: '='
        },
        link: function postLink(scope, elem, attrs) {
            scope.$watch('data', function (newValue, oldValue) {
                if (newValue === oldValue) { return; }
                elem.html(createUL(scope.data));
            });

            elem.html(createUL(scope.data));
        }
    };
});

And then use it like this: 然后像这样使用它:

<div id="output" ul-from-json data="data"></div>

See, also, this short demo . 另请参见此简短演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM