2016-04-22 108 views
0

這裏是我的isssue,我有兩個不同的循環,並且這些循環中的每一個都來自不同的http請求。這裏的什麼我已經得到了屏幕:附加兩個ng-重複自己

enter image description here

,我想什麼,就是,貝婁「克萊爾pagniez」我想「1」,波紋管「米歇爾Polnaref」我想3,波紋管「瑪蒂爾德齊默「我想要」1「。

現在,每個整數都反覆寫入,我不想這樣,我想讓它們在良好的元素下面。

這是我的代碼:

我認爲

<ul ng-repeat="user in userbyorga track by $index">{{user.name}}<li ng-repeat="item in numtickets track by $index">{{item}}</li></ul> 
在我的控制器

這裏我用顯示我的整數(票的數目),我需要2請求顯示一個元素:

$scope.displayuser = function(id){ 
      var token = "xxxxxxx"; 
      userdisplay 
       .send(token,id) 
       .then(function(data){ 
        console.log(data); 
        $scope.userbyorga = data.users; 
        console.log(data.users); 
        for(i = 0; i < data.users.length; i++){ 

         var userid = data.users; 


         var userarray = JSON.stringify(userid); 
         localStorage.setItem("myid",userarray); 
        } 
       }) 
       .then(function(){ 
        var tabuser = JSON.parse(localStorage.getItem("myid")); 
        console.log(tabuser); 
        var urls = []; 
        $scope.numtickets = []; 

        for(i = 0; i < tabuser.length; i++){ 



         urls.push({ 
          url:JSON.stringify("https://cubber.zendesk.com/api/v2/users/"+tabuser[i].id+"/tickets/requested.json") 

         }); 


         console.log(urls); 

         displayfilter 
          .user(token,tabuser[i].id) 
          .then(function(data){ 


           $scope.numtickets.push(data.tickets.length); 

           console.log($scope.numtickets); 





          }) 
        } 

       }) 

     }; 

Ang請求我在我的控制器中顯示tho se名稱:

$scope.displaycompany = function(){ 
      var token = "xxxxxxx"; 
      dropdown.send(token).then(function (data) { 

       $scope.usertab = data.organizations; 
       console.log($scope.usertab); 


      }); 

     } 

回答

1

有兩種方法可以做到這一點。首先是服務器端:有一個請求返回適當的數據集。

我將關注客戶端,因爲這是客戶端問題。我建議你創建一個列表包裹用戶和值:

var tabuser = JSON.parse(localStorage.getItem("myid")); 
    console.log(tabuser); 
    var urls = []; 
    $scope.data = []; 
    for(i = 0; i < tabuser.length; i++){ 
        var currentUser = tabuser[i];// defining a variable in the scope of the callback function tabuser[i] just won't work as expected in the .then 
        displayfilter 
         .user(token,tabuser[i].id) 
         .then(function(data){ 
          $scope.data.push({user:currentUser}, nbTickets:data.tickets.length}); 

          console.log($scope.data); 
         }) 
       } 

      }) 

現在你只需迭代對整個事情:

<ul ng-repeat="wrapper in data track by $index">{{wrapper.user.name}}<li ng-repeat="item in wrapper.nbTickets track by $index">{{item}}</li></ul> 

但是我不知道如果第二NG-重複是必要的。

如果這不符合您的需要檢查$q.all()文檔與您可以等待一堆承諾,並獲得所有結果在一起合併它們並重新格式化數據,以適應您的需求。

+0

我還沒試過,但是謝謝你給了我一個很好的答案! – xenurs