繁体   English   中英

角调用$ http.get同步

[英]Angular call $http.get synchronous

我在$ http中有一个使用get方法的代码

generateFooter: function(){
            var footer;
            $http.get('/angular-admin/personalize-tenant').then(function(response){
                    var tenant = {name: response.data.name,
                        address: response.data.addresses[0]
                    };
                    var address = '<div>'+ tenant.address.street_name + '</div>'
                    +   '<div>' + tenant.address.house_number + '</div>'
                    +   '<div>' + tenant.address.house_name + '</div>'
                    +   '<div>' + tenant.address.postcode + '</div>'
                    +   '<div>' + tenant.address.city + '</div>'
                    +   '<div>' + tenant.address.province + '</div>'
                    +   '<div>' + tenant.address.country + '</div>';
                    footer ={"cols": [{
                        "wd": 100,
                        "rows": [{
                            "text": tenant.name,
                            "filtertext": {
                                "0": tenant.name
                            },
                            "position": "cell",
                            "required": true,
                            "id": "tenant-name"
                        }
                        ]
                    }],
                        "position": "row",
                        "type": "footer"
                        };
                    return footer;
                });
            return footer;
        },

结果返回“ undefined”值。

我想在同步成功调用$ http.get方法时将返回页脚的值。

谢谢大家

希望这对您有用,

$http.get('/angular-admin/personalize-tenant').then(function(response){
                    var tenant = {name: response.data.name,
                    address: response.data.addresses[0]
                    };

而不是上面尝试下面的格式。

// Simple GET request example :
$http.get('/someUrl').
  success(function(data, status, headers, config) {
    //In data object you can access the result.
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

如果以上方法都不适合您,请告诉我您哪里有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM