Angular调用$ http.get同步

时间:2015-01-29 06:10:43

标签: javascript angularjs angularjs-service

我有一个剪辑代码,使用$ 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;
        },

结果返回&#34; undefined&#34;值。

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

全心全意,

1 个答案:

答案 0 :(得分:0)

我希望这对你有用,

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

如果以上对您不起作用,请告诉我您遇到问题的地方。