开发者问题收集

将数组变量传递给函数

2018-03-02
54

控制器内部有两个函数:

$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) 相同:

在此处输入图片说明

2个回答

问题在这里

  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++)
Sajeetharan
2018-03-02

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;
}  
Bimlendu Kumar
2018-03-02