如何从angularjs中的$ http函数中获取值

时间:2022-08-22 17:46:39

I've create a function in angular. And it is working fine and can get correct result but my problem is i want particular node from result. i have worked with JSON and Jquery, but i am new in angular.

我用角度创建了一个函数。它工作正常,可以得到正确的结果,但我的问题是我想要结果的特定节点。我曾与JSON和Jquery合作过,但我是角色新手。

This is my code.

这是我的代码。

$scope.booking= function(docid,addid){
    //console.log(addid);
    var a =  $http({
        method: 'POST',
        url: "http://localhost:81/DoctorStayServiceAngular/model/getDoctorBookingDetail.php",
        params:{"doctorid":docid,"addressid":addid,"day":weekday[d.getDay()]}
        //params:{"doctorid":docid}
    }).then(function mySucces(response) {
        //$scope.count= response.data;
        /*$scope.appo=[];

        $scope.appo= i;
        i++;*/
        //      
        //console.clear();
        return response.data;
    });
    console.log(a);
    return a;
};

http://localhost:81/DoctorStayServiceAngular/model/getDoctorBookingDetail.php

return following json.

返回json。

{
    "status":"success",
    "message":"",
    "booking":{
        "booking_detail":[{
            "session":null,
            "slot":null,
            "day":"Friday"
        }]
    }
}

This how i can see in firebug 如何从angularjs中的$ http函数中获取值

这是我在萤火虫中看到的

Expanded view 如何从angularjs中的$ http函数中获取值

so i want to get value(which I've highlighted in image) from response(which is under $$state), what should i do?

所以我想从响应(在$$状态下)获得价值(我在图片中突出显示),我该怎么办?

in short how can i get bottom result?

总之,我怎样才能得到最终结果?

{
    "status":"success",
    "message":"",
    "booking":{
        "booking_detail": [{
            "session":null,
            "slot":null,
            "day":"Friday"
        }]
    }
}

1 个解决方案

#1


1  

$http service returns a promise(this is printed in your console logs), which will be resolved after that http call is complete.

$ http服务返回一个promise(这将打印在您的控制台日志中),该http将在http调用完成后解析。

Please go through this $q documentation on how deferred data works.

请查看有关延迟数​​据如何工作的$ q文档。

https://docs.angularjs.org/api/ng/service/$q

This should work,

这应该工作,

$scope.booking= function(docid,addid){
        $http({
                method: 'POST',
                url: "http://localhost:81/DoctorStayServiceAngular/model/getDoctorBookingDetail.php",
                params:{"doctorid":docid,"addressid":addid,"day":weekday[d.getDay()]}
                //params:{"doctorid":docid}
            }).then(function mySucces(response) {
                $scope.responseData = response.data;
            });
        };

After this you can bind the data in responseData to your view.

在此之后,您可以将responseData中的数据绑定到您的视图。

#1


1  

$http service returns a promise(this is printed in your console logs), which will be resolved after that http call is complete.

$ http服务返回一个promise(这将打印在您的控制台日志中),该http将在http调用完成后解析。

Please go through this $q documentation on how deferred data works.

请查看有关延迟数​​据如何工作的$ q文档。

https://docs.angularjs.org/api/ng/service/$q

This should work,

这应该工作,

$scope.booking= function(docid,addid){
        $http({
                method: 'POST',
                url: "http://localhost:81/DoctorStayServiceAngular/model/getDoctorBookingDetail.php",
                params:{"doctorid":docid,"addressid":addid,"day":weekday[d.getDay()]}
                //params:{"doctorid":docid}
            }).then(function mySucces(response) {
                $scope.responseData = response.data;
            });
        };

After this you can bind the data in responseData to your view.

在此之后,您可以将responseData中的数据绑定到您的视图。