从 AngularJS 中的另一个函数调用回调函数



大家早上好,

我正在尝试从 angularjs 中的花药函数调用回调函数。

我的控制器看起来像

 function LMSAppController($scope, LMSAppFactory,$http) {
       $scope.branchSearch = function (code){
        $scope.hidbranchcode = code;
        $scope.gridOptions = {};
    }
    $scope.gridOptions = {
        getData: LMSAppFactory.getTableData,    
    };
}

当我尝试从branchSearch调用gridOptions函数时,gridOptions不调用。请告诉我我的错误在哪里?提前谢谢。

编辑:

function LMSAppFactory($http) {
        var ajaxRequest = 'processRequest.jsp?';
        var branchCode = document.getElementById("hidbranchcodeID").value ;
        alert("branchCode===="+branchCode);
       return {    
            getTableData: getTableData,
       }; 
       function getTableData(params, callback) {
            $http.get(ajaxRequest + 'requestType=getRecords'+params+'&value=10').then(function (response) {
                callback(response.data[1].LMSRecords, response.data[0].LMSRecordsCount);
            });
        }
    }

现在函数正在调用..非常感谢@Jaromanda X先生。但是现在我在控制台中得到了" Error: response.data[1] is undefined ">

gridOptions

是一个函数。 它是一个对象。当您调用$scope.gridOptions = {};时,它会将gridOptions的值重置为空对象。如果你想做gridOptions函数,那就这样改变它。

$scope.gridOptions = function(){
  return {
      getData: LMSAppFactory.getTableData
  }
}

现在像这样调用gridOptions函数方法getData

$scope.branchSearch = function (code){
    $scope.hidbranchcode = code;
    $scope.gridOptions().getData();
}

可能是你的response.data是一个array of single object。 像下面这样:

var data = [
             {}
           ];

因此,如果您尝试访问data[1]它将返回 undefined .

演示

var data = [{}];
console.log(data[1]); // undefined

最新更新