【问题标题】:Display nested results from http request after submit提交后显示来自 http 请求的嵌套结果
【发布时间】:2017-02-10 18:30:47
【问题描述】:

我的页面上有一个文本框,用户可以在其中输入他们想要搜索的用户名,然后单击提交按钮。我想要实现的是当用户点击提交按钮时,结果应该立即推送到页面。 我的脚本只显示一个结果,即使可能有 10 个或更多

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


    }])

页面上什么都没有显示

【问题讨论】:

  • 你在data得到了什么?
  • 我得到了接收者和金额的嵌套结果。多个结果
  • 我知道了。但是你能在某个地方粘贴它并显示你到底得到了什么吗?

标签: javascript html angularjs


【解决方案1】:

如果第一个脚本显示一个结果,我假设您的数据采用格式

[{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>

【讨论】:

  • 数据不止一个
  • 你真的需要先告诉我数据的结构,然后我才能回答这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2016-09-14
相关资源
最近更新 更多