角度资源覆盖 URL 不起作用



这里的文档说:

url – {string} – action specific url override. 
The url templating is supported just like for the resource-level urls.

我想使用这个好功能,我试过这个:

angular.module("experience", ['ngResource'])
    .factory('Experience', function ($resource, identity) {
    return $resource("api/experiences/:id", {}, {
        queryAll : {
            method  : 'GET',
            url     : 'api/companies/' + identity.id + '/experiences',
            isArray : true
        }
    });
});

您会看到我正在尝试覆盖查询所有方法的网址。 但这不起作用,查询仍会发送 URL API/体验。 这真的受支持还是我做错了什么? 感谢您的任何帮助。

我的项目中有一个非常相似的问题。无法在资源操作中覆盖 URL。我正在使用 Angular 1.2.0,它应该从 1.1.4 开始支持该功能。所以我检查了Angular API参考,CHANGELOG,但没有运气。然后我深入研究了源代码,注意到覆盖 url 的逻辑不存在。这就是问题的根本原因。我将 Angular 更新到 1.2.0,但我忘记将angular-resource.js更新到相应的版本。

所以,长话短说:检查角度资源的版本.js

我不确定您是否无法访问 url 覆盖中identity的属性,但您可以尝试这样的事情:

return $resource("api/experiences/:id", {}, {
    queryAll : {
        method  : 'GET',
        url     : 'api/companies/:id/experiences',
        params  : { id: identity.id },
        isArray : true
    }
});

id: identity.id告诉 angular 使用 id 属性 identity (如果存在)。

最新更新