2016-10-20 11 views
5

需要幫助以瞭解我的錯誤/在頁面上顯示數據。ng-repeat在通過工廠服務提取時不顯示數據

我經歷了幾乎所有論壇上的問題,都說「ng-repeat not working」。但仍然無法找出答案。

(請注意:我第一次嘗試用$ HTTP創建工廠服務來獲取數據)

請看看我下面的JavaScript代碼(模塊,控制器和工廠在一個地方定義)

//App declaration 
    var myApp = angular.module("appLeadsLogRpt", []); 

    //Controller declaration 
    myApp.controller('controllerLeadsLogRpt', ['dataService', fncontrollerLeadsLogRpt]); 

    function fncontrollerLeadsLogRpt(dataService) { 
     var vm = this; 

     //Table headers 
     vm.TableHeaders = ["Lead Id", "Source", "Create Date", "Status", "Contact Id", "Customer Name", "AssignedTo", "Mail Content", "Closed Reason", "Last Lead Note"];    

     dataService.getAllData() 
      .then(getData,null) 
      .catch(showError); 

     function getData(data) { 
      vm.LeadsLogRptData = JSON.parse(data); 
      //console.log(JSON.parse(data)); 
     } 
     function showError(errMsg) { 
      console.log(errMsg); 
     } 
    } 

    //Factory Service to fetch data 
    myApp.factory('dataService', ['$http', DataService]); 

    function DataService($http) {    
     return { 
      getAllData: GetAllData 
     }; 

     function GetAllData() {     
      return $http({ 
       method: 'get', 
       url: 'DataHandler.ashx?method=leadsReport&listId=504473' 
      }) 
      .then(sendResponseData) 
      .catch(sendError) 
     } 

     function sendResponseData(response) {     
      return response.data; 
     } 
     function sendError(response) { 
      return response.status; 
     } 
    } 

</script> 

而且我的HTML象下面這樣:

<div id="DvContent" style="width:100%" data-ng-app="appLeadsLogRpt"> 
    <div style="width:100%;" data-ng-controller="controllerLeadsLogRpt as vm1"> 
     <input type="text" id="txtJsonData" value='<%=jsonLeadsLogRpt %>' style="display:none" /> 

     <div><h3>Leads Log Report</h3></div> 
     <div style="text-align:right;margin-bottom:2px;padding-right:2px;"> 
      <a href="javascript:void(0);" data-ng-click="ExportToExcel();"><img src="/mp_images/excelicon.gif" border=0 width=22 height=22 alt="Open in Excel"></a>      
     </div> 
     <div id="divExportToExcel"> 
      <table style="width:100%" id="tblLeadLogRpt" class="table table-bordered"> 
       <thead> 
        <tr style="background-color:#CCCCCC"> 
         <th data-ng-repeat="item in vm1.TableHeaders" class="ng-cloack">{{item}}</th> 
        </tr> 
       </thead> 
       <tbody>       
        <tr data-ng-repeat="item1 in vm1.LeadsLogRptData"> 
         <td class="ng-cloack">{{item1.A_LeadId}}</td>       
         <td class="ng-cloack">{{item1.B_Source}}</td>       
         <td colspan="8"></td>       
        </tr> 
        <tr data-ng-if="LeadsLogRptData.length==0"><td colspan="10">Data Not Found</td></tr>       
       </tbody> 
      </table> 
     </div> 
    </div> 
</div> 

如果我將服務器返回的硬編碼數據分配給ng - 重複它可以正常工作

請讓我知道我做錯了什麼。

還有一個問題。

在工廠我通過調用$ HTTP

如果我想用Post方法稱之爲get方法,我怎麼傳PARAMS獲取數據?

在jquery中,我按照下面的方式來做。

$.ajax({ 
     url: 'AbcdHandler.ashx', 
     type: 'POST', 
    data: { 
      'method': 'ABCData', 
      'StartDate': startDate, 
      'EndDate': endDate 
    }, 
    success: function (result) { 
      return JSON.parse(result); 
    }, 
    error: OnError 
}); 

在此先感謝您的閱讀和幫助。

我最近的觀察:

當我在控制檯上寫數據,得到了這個。 (看看函數getData(data))

[{「A_LeadId」:「426429」,「B_Source」:「LabX」},{「A_LeadId」:「429369」,「B_Source」:「LabX」 },{ 「A_LeadId」: 「430586」, 「B_Source」: 「信息」},{ 「A_LeadId」: 「430589」, 「B_Source」: 「信息」},{ 「A_LeadId」: 「433848」, 「B_Source」 : 「LabX軟件」},{ 「A_LeadId」: 「448592」, 「B_Source」: 「信息」},{ 「A_LeadId」: 「451795」, 「B_Source」: 「投標」},{ 「A_LeadId」: 「453008」 ,「B_Source」:「低投標」},{「A_LeadId」:「453009」,「B_Source」:「低投標」},{「A_LeadId」:「453010」,「B_Source」 「A_LeadId」: 「455736」, 「B_Source」: 「信息」},{ 「A_LeadId」: 「455743」, 「B_Source」: 「信息」},{ 「A_LeadId」: 「457030」, 「B_Source」:「信息「},{」A_LeadId「:」457052「,」B_Source「:」LabX「},{」A_LeadId「:」461503「,」B_Source「:」手動輸入「}]

如果我直接複製分配給vm.LeadsLogRptData,系統顯示我在屏幕上輸出pro佩爾利。 例如vm.LeadsLogRptData = [{「A_LeadId」:「426429」,「B_Source」:「LabX」},......];

還有一件事。當我檢查數據的長度,它顯示我621({{vm.LeadsLogRptData.length}})

實際上僅存在15大括號對({})和系統應該顯示我長度爲15

希望我解釋/正確描述我的問題。

(需要一些東西,正確轉換JSON格式我的數據)

感謝,

我回答

只需使用eval與Json.parse和系統開始顯示數據正常 例如vm.LeadsLogRptData = eval(JSON.parse(data));

任何人都請解釋我背後的邏輯?

感謝每一位閱讀和答覆到目前爲止。

+0

你不太清楚你的意思是「如果我通過服務器分配硬編碼數據返回」。你在那個控制檯語句中得到了什麼'console.log(JSON.parse(data));'? – Sajan

+0

Sajan,請查看「我的最新觀察」部分。希望能幫助你理解我的問題。謝謝 – user3172224

回答

0

也許你應該使用$ HTTP對象的params屬性,如:

function GetAllData() {     
      return $http({ 
       method: 'GET', 
       url: 'http://localhost:12345/DataHandler.ashx', 
       params: {method: "leadsReport", listId: 504473} 
      }) 
      .then(sendResponseData) 
      .catch(sendError) 
     } 

如果要執行POST請求,你應該使用這樣的:

function PostData() {     
      return $http({ 
       method: 'POST', 
       url: 'http://localhost:12345/DataHandler.ashx', 
       data: {exampleData: exampleData} 
      }) 
      .then(sendResponseData) 
      .catch(sendError) 
     } 

記住更改http://localhost:12345/爲您的服務器URL。

+0

Kacper,謝謝你的幫助。但是,如果我想發佈我的表單字段,我該如何傳遞它們?如果我想傳遞兩個輸入字段和一個單選按鈕值,我如何將它們壓縮在「exampleData」中?此外,我必須能夠通過使用Request對象在處理程序頁面上檢索它們。請指導。 – user3172224

+0

例如,如果你有像'$ scope.user.name','$ scope.user.surname'這樣的字段的'$ scope.user'的視圖模型對象,並且你想將它傳遞給** $ http **數據屬性,你可以做'data:{exampleData:$ scope.user}',或'data:{exampleData1:$ scope.user.name,exampleData2:$ scope.user.surname}' –

+0

Kacper謝謝你的解釋。代碼看起來與我目前在JQuery中使用的非常相似,除非參數名不需要單引號(')。將嘗試這一點,如果遇到任何問題,請再次求助。謝謝 – user3172224

相關問題