数据来自本地json文件,但我只希望在输入字段中键入searchText(在按键上)时发出http.get请求。

  <input type="text" class="form-control" ng-model="searchText">


我将在下表中显示结果:

<table ng-if="searchText" class="searchResults table table-bordered table-hover table-responsive">
  <tr>
    <th>DLL ID</th>
    <th>KVK Nummer</th>
    <th>Company Name</th>
  </tr>

  <tr ng-repeat="data in getData | filter: customFilter">
   <td>{{data.Id}}</td>
   <td>{{data.stTax_ID_VAT_Number}}</td>
   <td>{{data.AccountName}}</td>
</table>


到目前为止,在控制器中,我有以下内容:

    $scope.searchText = undefined;

    if($scope.searchText){
    $http.get('data/json_sample/DV_IC_01.json')
            .success(function(data){
                $scope.allData = data;
                $scope.getData = $scope.allData.d.results;
            });
}
    $scope.customFilter = function(row){
        if (!$scope.searchText){
                return true;
            }

        return (row.stTax_ID_VAT_Number.indexOf($scope.searchText) !== -1) || (row.AccountName.toLowerCase().indexOf($scope.searchText.toLowerCase()) !== -1);
    }

最佳答案

我会使用指令ng-change

<input type="text" class="form-control" ng-model="searchText" ng-change="getData()">


在控制器内部:

$scope.getData = function(){
  //implement your $http logic.
}

10-07 22:00