$http issue - Values can't be returned before a promise is resolved in md-autocomplete Angular Material

前端 未结 4 1941
别那么骄傲
别那么骄傲 2021-01-03 06:06

I\'m using Angular Material md-autocomplete in my project. In that I\'m getting the suggest listing from the Service Host via ajax call using $http

4条回答
  •  迷失自我
    2021-01-03 06:45

    Why not just put return countryList inside the success function.

    function LoadAutocomplete(id) {
        var countryList = [];
        $http({
                method: "post",
                url: "https://www.bbminfo.com/sample.php",
                params: {
                    token: id
                }
            })
            .success(function (response) {
                countryList = response.records;
                return countryList;
            })
            .error(function (response) {
                countryList = [];
                return countryList;
            });
    }
    

    edit due to deprecation of .success and .error methods:

    function LoadAutocomplete(id) {
        var countryList = [];
        $http({
                method: "post",
                url: "https://www.bbminfo.com/sample.php",
                params: {
                    token: id
                }
            })
            .then(function (response) {
                countryList = response.data.records;
                return countryList;
            },function () {
                countryList = [];
                return countryList;
            });
    }
    

提交回复
热议问题