2016-05-31 32 views
0

我想綁定「線程」和「評論」。角度嵌套ng-repeat與服務器端數據

  • 線程1
    • 註釋1
    • 評論2
    • 評論3
  • 線程2
    • 註釋1
    • 評論2
  • 線程3
    • 註釋1
    • 評論2
    • 評論3
    • 評論4
    • 註釋5

我必須取「螺紋「和」評論「從數據庫服務。線程服務提供所有線程的數據,而「評論」服務取得線程ID並僅返回傳遞的線程ID註釋。

那麼,如何通過拖拽單獨的服務來綁定上述嵌套列表中的數據。

app.controller('ThreadCtrl', function ($scope, $http, $location) { 
 
    $scope.Threads = []; 
 
    $scope.Comments = []; 
 

 
    $http({ 
 
     method: 'POST', 
 
     url: 'api/ThreadList' 
 
    }).success(function (data) { 
 
     $scope.Threads = JSON.parse(data); 
 
    }); 
 
    
 
    $http({ 
 
     method: 'POST', 
 
     url: 'api/CommentList', 
 
     data: JSON.stringify({ ThreadID: 0 }) //The problem is, ThreadID will resolve at runtime 
 
    }).success(function (data) { 
 
     $scope.Threads = JSON.parse(data); 
 
    }); 
 
})

+0

,如果您能提供樣品JSON(我假設你的服務返回JSON)的什麼都服務返回我可能能夠幫助你與你的任務 –

+0

我還注意到,你將這兩個請求的數據保存到$ scope.Threads,我確實認爲第二個壽命ld保存到$ scope.Comments。 –

+0

在我看來,你應該從後端處理所需的格式,因爲你需要在線程對象內嵌套註釋對象數組。不建議在前端處理它 –

回答

1

抓取success處理您的threads呼叫內從服務器的評論:

app.controller('ThreadCtrl', function ($scope, $http, $location) { 
    $scope.Threads = []; 
    $scope.Comments = []; 

    $http({ 
     method: 'POST', 
     url: 'api/ThreadList' 
    }).success(function (data) { 
     $scope.Threads = JSON.parse(data); 

     // iterate over the threads here (this is where they are available already) and fetch the comments by IDs... 

     var thread = ...; 

     $http({ 
      method: 'POST', 
      url: 'api/CommentList', 
      data: JSON.stringify({ ThreadID: thread.id }) 
     }).success(function (data) { 
      thread.comments = JSON.parse(data); 
     }); 
    }); 
})