Angular ng-repeat with nested json objects?

后端 未结 5 2077
鱼传尺愫
鱼传尺愫 2020-12-30 09:49

I have a JSON object, represented as such:

 {
  \"orders\" : [
    {
      \"ordernum\" : \"PRAAA000000177800601\",
      \"buyer\" : \"Donna Heywood\"
              


        
5条回答
  •  没有蜡笔的小新
    2020-12-30 10:25

    Searching a lot for nice and simple solution for iterating dynamically. I came up with this

    JAVASCRIPT (angular): a person is an example of nested object. the is_object function will be use in the HTML view.

    $scope.person = {
        "name": "john",
        "properties": {
           "age": 25,
           "sex": "m"
        },
        "salary": 1000
    }
    
    // helper method to check if a field is a nested object
    $scope.is_object = function (something) {
        return typeof (something) == 'object' ? true : false;
    };
    

    HTML: define a template for simple table. the 1st TD is the key which is displayed. another TD (2 or 3, but never both) will be show the value if its not an object (number / string), OR loop again if its an object.

    {{ k }} {{ v }}
    {{ k2 }} {{ v2 }}

提交回复
热议问题