Display nested results from http request after sending

on my page there is a text box where users enter the username that they want to execute, and after clicking the submit button. what I want to achieve is when the user clicks the submit button, the results should be immediately clicked on the page. With my script, what it does displays only one result, although there may be 10 or more

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>

When I change my script to this:

  .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);
            });
            }


    }])

Nothing is displayed on the page

+4
source share
1 answer

If the first script displays a single result, I assume that your data comes in the format

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

so you can do $scope.searchResults = data;

and in html, do

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

Source: https://habr.com/ru/post/1656529/


All Articles