2016-10-02 81 views
4

在我的页面上有一个文本框,用户输入他们想要搜索的用户名,然后单击提交按钮。我想要实现的是当用户点击提交按钮时,结果应立即推送到页面。 随着我的脚本它的作用是显示只有一个结果,即使有可能是10个或更多提交后显示来自http请求的嵌套结果

JS

.controller('search_ctrl',['$scope','$http','$location','$ionicLoading','$ionicPopup','$state','$stateParams','$cordovaToast',function($scope,$http,$location,$ionicLoading,$ionicPopup,$state,$stateParams,$cordovaToast){ 
    $scope.username=localStorage.getItem("username"); 
    $scope.searchresults = []; 


     $scope.expendsearch=function() { 
     $ionicLoading.show({template: '<p>Please Wait...</p><ion-spinner></ion-spinner>'}); 
     event.preventDefault(); 
     $http.post("http://localhost/app/templates/spree/sales/expenses/search_results.php", 
     {'username':$scope.username}) 
     .success(function(data){ 
     //console.log(JSON.stringify(data)); 
     $scope.results=data; 
     {$ionicLoading.hide();} 

     var search ={"fullname":data[0].reciever, "username":data[0].amount}; 
     $scope.searchresults.push(search); 
     }).error(function(error){ 
     console.error(error); 
     }); 
     } 


}]) 

HTML

<div ng-controller="search_ctrl" ng-repeat="item in searchresults track by $index"> 
{{item.username}} 
{{item.fullname}} 
</div> 

当我将剧本改成这样:

.controller('search_ctrl',['$scope','$http','$location','$ionicLoading','$ionicPopup','$state','$stateParams','$cordovaToast',function($scope,$http,$location,$ionicLoading,$ionicPopup,$state,$stateParams,$cordovaToast){ 
     $scope.username=localStorage.getItem("username"); 
     $scope.searchresults = []; 


      $scope.expendsearch=function() { 
      $ionicLoading.show({template: '<p>Please Wait...</p><ion-spinner></ion-spinner>'}); 
      event.preventDefault(); 
      $http.post("http://localhost/app/templates/spree/sales/expenses/search_results.php", 
      {'username':$scope.username}) 
      .success(function(data){ 
      //console.log(JSON.stringify(data)); 
      $scope.results=data; 
      {$ionicLoading.hide();} 

      var search ={"fullname":data.reciever, "username":data.amount}; 
      $scope.searchresults.push(search); 
      }).error(function(error){ 
      console.error(error); 
      }); 
      } 


    }]) 

没有在页面上出现

+0

你是什么进入'数据'? – Pradeepb

+0

我得到嵌套结果的收件人和金额。不止一个结果 – user6579134

+0

我已经知道了。但是,你可以在某处粘贴它并显示你正在得到什么? – Pradeepb

回答

0

如果第一个脚本显示一个结果,我想你的数据来自格式

[{reciever:'aaa',amount:'bbb'},{reciever:'ccc',amount:'ddd'} ....] 

所以你可以做$scope.searchResults = data;

,并在html,做

<div ng-controller="search_ctrl" ng-repeat="item in searchresults track by $index"> 
{{item.reciever}} 
{{item.amount}} 
</div> 
+0

有多个数据 – user6579134

+0

你真的需要告诉我的数据结构之前,我可以回答 –