2017-06-22 77 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变量中,那么您可以在该控制器内的任何位置访问它 –

相关问题