2017-06-21 90 views
1

我有一個複雜的JSON對象,我必須通過它循環並呈現表單中的數據。對於我創建了一個小的服務是這樣的:使用angularjs循環瀏覽json

service.myApprovalResults = function(){ 
     var url = "../data/myapprovals.json"; 
     return $http.post(url).success(function(response) { 
     $rootScope.names = response; 
     }); 
    }; 

以下是我的JSON文件:

{ 
    "result": [ 
     { 
        "Approval": { 
         "Id":"111",  // Unique Id 
         "Api":"api/sometihgn/111" 
        }, 
        "Ticket":{ 
        "Id":"TKT1000011", 
        "API":"api/ticket/TKT1000011" 
        }, 
        "TicketItem":{ 
        "Id":"RITM1000011", 
        "API":"api/ticket_item/RITM1000011" 
        }, 
        "ProcessStep":"", 
        "Description":"Need Approval For sdsadas", // See Note Section 
        "VariablesSet":// See Note Section 
         [   // VariableSet details. 
          { 
           "Question":"Joining Date", 
           "Name": "joining_date", 
           "Id":"13", 
           "Value": ["12:12:2014"], 
           "DisplayValue":[""], 
           "Reference":"" 
          }, 
          { 
           "Question":"Reporting Manager", 
           "Name": "reporting_manager", 
           "Id":"14", 
           "Value": ["samd"], 
           "DisplayValue":["/Sam Doni"], 
           "Reference":"/api/user" 
          }, 
          { 
           "Question":"Department", 
           "Name": "department", 
           "Id":"15", 
           "Value": ["IT Security"], 
           "DisplayValue":[""], 
           "Reference":"" 
          }, 

          { 
           "Question":"Accessories Needed?", 
           "Name": "accessories", 
           "Id":"16", 
           "Value": ["laptop","mouse"], 
           "DisplayValue":[""], 
           "Reference":"" 
          } 
         ],        
        "ApprovalFor": { 
         "Name": "Sam Doni", 
         "UserName": "samd", 
         "Email":"[email protected]", 
         "Api": "api/user/samd" // Api can be used to get more details 
        }, 
        "Approver":{ // Optional 
         "Name": "sdsdsdsad", 
         "UserName": "sdsdsad", 
         "Email":"[email protected]", 
         "Api": "api/user/dssds" // Api can be used to get more details 
        }, 
        "State": "Requested", 
        "Group": "",// Optional 
        "CreatedBy": { 
         "Name": "sadsdsddsa", 
         "UserName": "sdsd", 
         "Email": "[email protected]", 
         "Api": "api/user/sadsadsa" 
        }, 
        "CreatedOn": "2016-11-09T17:29:01Z", 
        "UpdatedOn": "2016-11-09T17:29:01Z", 
        "CommentsSet": [{ 
          "Id": "10", 
          "TicketItem": "RITM1001011", 
          "Type": "approvalnote", 
          "Value": "User has issues related to access", 
          "CreatedOn": "2016-11-09T17:29:01Z", 
          "CreatedBy": { 
           "Name": "sdsdsadsadd", 
           "UserName": "dsdsad", 
           "Email":"[email protected]", 
           "Api": "api/user/ssddsad" 
          } 
         }, 
         { 
          "Id": "11", 
          "TicketItem": "RITM1001011", 
          "Type": "approvalnote", 
          "Value": "Issue is solved", 
          "CreatedOn": "2016-11-08T17:29:01Z", 
          "CreatedBy": { 
           "Name": "dsadsdsad 
           "UserName": "sdsadas", 
           "Email":"[email protected]", 
           "Api": "api/user/sadsadsadsa" 
          } 
         } 
        ] 
       }, 
       { 
        "Approval": { 
         "Id":"112",  
         "Api":"api/approval/112" 
        }, 
        "Ticket":{ 
        "Id":"TKT1000013", 
        "API":"api/ticket/TKT1000013" 
        }, 
        "TicketItem":{ 
        "Id":"RITM1000021", 
        "API":"api/ticket_item/RITM1000021" 
        }, 
        "ProcessStep":"", 
        "Description":"Add user to Helpnow Group",// See Note Section 
        "VariablesSet":// See Note Section 
         [ 
          { 
           "Question":"Location Service Needed", 
           "Name": "location", 
           "Id":"16", 
           "Value": ["11"], 
           "Reference":"/api/location"; 
          }, 
          { 
           "Question":"What is the issue", 
           "Name": "service_needed", 
           "Id":"18", 
           "Value": ["aloha"] 
           "DisplayValue":[""], 
           "Reference":"" 
          } 
         ],      
        "ApprovalFor": { 
         "Name": "dsfdsfdsf", 
         "UserName": "dfdsf 
         "Email":"[email protected]", 
         "Api": "api/user/rsds// Api can be used to get more details 
        }, 
        "Approver":"", 
        "State": "Requested", 
        "Group": { 
          "Name": "FIN Group", 
          "Id":"10" 
          "Api": "api/group/10"// Unique Group ID in SAP 
         } 

      } 
    ] 
} 

使用這個JSON我需要遍歷和形式顯示JSON數據。並且我在下面添加了以下代碼:

<ul> 
     <li ng-repeat="x in names"> 
      {{ x.result.Approval.Id }}, {{ x.result.Ticket.Id }}, {{x.result.TicketItem.Id}}, {{x.result.Description}} 
     </li> 
    </ul> 

完成上述操作但我無法看到表單中的數據。

我檢查了Chrome開發人員工具,JSON加載正常。 有人可以建議我嗎?

+0

NG-調整你的JSON和響應存儲重複只適用於數組,而不適用於對象。 – Ved

+0

仔細檢查JSON響應是否有效 –

+0

@Ved - 'ng-repeat'也可以用於對象。 – 31piy

回答

0

你錯誤地遍歷你的數據。我相信你想在你的數組中顯示細節。您需要將x in names更改爲x in names.result。然後,改變你的表現直接使用x這樣的:

{{ x.Approval.Id }}, {{ x.Ticket.Id }}, {{x.TicketItem.Id}}, {{x.Description}} 
0

第一件事,檢查是否已指定$rootScope.names作爲而declaratiion空數組。如果是這種情況,儘可能直接聲明並分配它,以避免數據結構的進一步複雜化。

根據您的JSON結構中的代碼應該是這樣的:

<ul> 
<li ng-repeat="x in names.result"> 
    {{ x.Approval.Id }}, {{ x.Ticket.Id }}, {{x.TicketItem.Id}}, {{x.Description}} 
</li> 
</ul> 

此外,爲了避免這種複雜性,試圖從下一次:)

+0

Sriram,在響應進入之前我已經分配給一個空數組,並且當我打開DOM時,它簡單地給了我這個標記<! - ngRepeat:x in names.result - > – youi

+0

嘗試修改'names.result'到'$ root.names.result.' 如果這不起作用,只需嘗試記錄:'$ rootScope.names.result [0] .Approval.Id',以驗證我建議的解析是否正確。 –

+0

嘗試了這個$ rootScope.names.result [0] .Approval.Id,但得到了這個未捕獲的ReferenceError:$ rootScope未定義 在:1:1 – youi