AngularJS RESTful 参数

22 浏览
0 Comments

AngularJS RESTful 参数

是否可能使用AngularJS的$http服务来给restful url加上参数?例如:

var URL = '/api/countries/:countryId/states'
var getStates = function (countryId) {
    $http.get(URL, { countryId: countryId }).then();
};

我尝试的每一种组合最终都使用查询字符串,这不是我想要的。

admin 更改状态以发布 2023年5月21日
0
0 Comments

正如 Rathish 和 Tony 所指出的,您可以使用 $resource:

var URL = '/api/countries/:countryId/states'
var states = $resource(URL)
var getStates = function (countryId) {
  states.query({ countryId: countryId }).then(...)
};

0
0 Comments

我认为按照您的描述这是不可能实现的。

最终,您可能只需要采用以下方式:

var getStates = function (countryId) {
    var URL = '/api/countries/'+countryId+'/states'
    $http.get(URL, {}).then();
};

如果有人有更好的解决方案,我会很乐意听取建议:)

0