2017-06-22 91 views
0

如何比較在angularjs中的兩個不同函數中聲明的兩個不同的對象數組?

var app2 = angular.module("webApp2", []) 
 
    .controller("webCtrl2", function ($scope, $state, $http) { 
 
     console.log("hi"); 
 
     $scope.username = $state.params.username; 
 
     console.log("shbsdjh" + $scope.email) 
 
     $scope.userId = $state.params.userid; 
 
     $scope.requests = []; 
 
     var addedList = []; 
 

 
     //Getting requests list: 
 
     $http.get("http://192.168.2.3:3000/userslist") 
 
      .then(function (response) { 
 
       console.log("hi", JSON.stringify(response)); 
 
       $scope.requests = response.data; 
 
      }, function (response) { 
 
       console.log("error" + response); 
 
      }); 
 

 
     $scope.addfrnd = function (reqname, index) { 
 
      console.log("shgsgh" + reqname); 
 
      var data = { 
 
       userId: $scope.userId, 
 
       requestname: reqname, 
 
       username: $scope.username 
 
      } 
 
      var req = { 
 
       method: 'POST', 
 
       url: 'http://192.168.2.3:3000/friendrequests', 
 
       data: data 
 
      } 
 
      $http(req).then(function (response) { 
 
       console.log("saghdha" + JSON.stringify(response.data)); 
 
       //.......................................................................................... 
 
       //Gettind added list 
 
       $http.get("http://192.168.2.3:3000/sendfriendrequests/" + $scope.username) 
 
        .then(function (response) { 
 
         console.log("added list" + JSON.stringify(response)); 
 
         addedList = JSON.stringify(response.data.username); 
 
         console.log("addedlist" + addedList) 
 
        }, function (response) { 
 
         console.log("error" + response); 
 
        }); 
 
       //.......................................................................................... 
 

 
      }, function (response) { 
 
       console.log(response); 
 
      }); 
 
     } 
 
     console.log("uname" + addedList); 
 
     $scope.logoutFn = function() { 
 
      $state.go("signup"); 
 
     } 
 

 
    });

蔭與聊天application.Here蔭越來越userslist和使用API​​s.In的userslist API發送請求列表的工作,人一個數組列表,將獲得和發送請求API,數組列表那些我發送請求的人將會出現。現在我必須比較這兩個包含對象的數組,以便將這些人移除到請求在userslist中發送的人.Iam在實現angularjs中的代碼時比較這些兩個數組聲明爲兩個不同的函數。請幫助我.. 這是我的javascript代碼:

+0

你可以發表相同的代碼嗎? – Vivz

+0

https://stackoverflow.com/questions/44698818/how-to-compare-two-arrays-which-are-in-different-functions/44699054#44699054 – Vivz

回答

0

試試這個

angular.forEach(arr1, function(value1, key1) { 
     angular.forEach(arr2, function(value2, key2) { 
      //Comparison logic 
     }); 
    }); 
+0

實際上,數組數據獲取僅限於其get方法函數。那麼我該如何在函數外部使用該數組呢? – srujana

+0

如果您將數組都存儲在$ scope變量中,那麼您可以在該控制器內的任何位置訪問它 –

相關問題