将数组变量传递给函数

时间:2018-03-02 04:45:27

标签: angularjs

我在控制器中有两个功能:

$scope.historyMemberInit = function(fkhouseid) {
  $scope.fkhouseid = fkhouseid;
  $http({
    method: 'post',
    url: 'House/member_list.php',
    data: {fkhouseid:$scope.fkhouseid}
  }).then(function successCallback(response) {
    $scope.memberList = response.data;
    $scope.remains($scope.memberList);      
    console.log(response);
  });
};
$scope.historyHouseInit = function(fkhouseid) {
  $scope.fkhouseid = fkhouseid;
  $http({
    method: 'post',
    url: 'House/house_list.php',
    data: {fkhouseid:$scope.fkhouseid}
  }).then(function successCallback(response) {
    $scope.houseList = response.data;
    $scope.remains($scope.houseList);
    console.log(response);
  });
};  

它将从数据库为两个不同的表加载数据。我想在每个函数中调用$scope.remains();函数:

$scope.remains = function(list) {   
  $scope.unList = list;   
  $scope.limit = 15;
  var stubs = [];
  for (var i = 0; i < $scope.limit - $scope.unList.length; i++)
    stubs.push(i);    
  return stubs;
}   

我得到错误:

TypeError: Cannot read property 'length' of undefined

如何将列表变量传递给remains函数?

修改 以下是console.log($scope.houseList);console.log(response.data)相同的内容:

enter image description here

2 个答案:

答案 0 :(得分:0)

问题在这里

  for (var i = 0; i < $scope.limit - $scope.unList.length; i++)

分配变量并进行比较,

var difference = $scope.limit - $scope.unList.length;

然后,

  for (var i = 0; i < difference ; i++)

答案 1 :(得分:0)

Response.data推入某个数组,然后发送到函数: (因为你的响应不是数组,它是一个对象)

var arrayObject=[];
arrayObject.push(response.data[0]);
then call function:
$scope.remains = function(arrayObject) {   
  $scope.unList = arrayObject;   
  $scope.limit = 15;
  var stubs = [];
  for (var i = 0; i < $scope.limit - $scope.unList.length; i++)
    stubs.push(i);    
  return stubs;
}