gpt4 book ai didi

angularjs - Angular.js 中的 $resource 关系 [更新]

转载 作者:行者123 更新时间:2023-12-01 01:17:48 25 4
gpt4 key购买 nike

I'm trying to figure out the best way to write a model that relates to other models, such as an Order that has 1 or more OrderItems.



加载订单时如何获取相应的订单项?
angular
.module('MyApp.services', ['ngResource'])
.factory('Order', function($resource) {
return $resource('/api/v1/Order/:orderId?format=json', {}, {});
});
.factory('OrderItem', function($resource) {
return $resource('/api/v1/OrderItem/:orderitemId?format=json', {}, {});
});

我在 get Order 上尝试了一个回调函数来加载 OrderItems,但是没有用。

有一个非常相似的问题,但它已过时: $resource relations in Angular.js

最佳答案

您可以将该函数包装在另一个获取订单项的函数中吗?

myApp.factory('Order', function($resource) {
var res = $resource('/api/Order/:orderId', {}, {
'_get': { method: 'GET' }
});

res.get = function(params, success, error) {
return res._get(params, function(data) {
doOrderItemStuff();
success(data);
}, error);
}
return res;
}

关于angularjs - Angular.js 中的 $resource 关系 [更新],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11038425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com