gpt4 book ai didi

javascript - AngularJS - 始终有一个参数作为资源的默认值

转载 作者:行者123 更新时间:2023-12-02 19:26:06 25 4
gpt4 key购买 nike

基本上,我遇到了一个场景,在很多情况下,我正在访问的 API 使用 :groupId 作为 url 的一部分。有没有一种简单的方法可以避免在每个函数调用中传递 $routeParams.groupId?

var app = angular.module('plunker', []);

app.config(["$routeProvider", function($routeProvider) {
$routeProvider.
when("/:groupId/location", {templateUrl: "partials/location.html", controller: "LocationCtrl"});
}]);

/* Controllers */
app.controller('LocationCtrl', function($scope, $routeParams, Location) {
Location.groupId = $routeParams.groupId; /* Is this even possible? I want to have it available to Location instead of the below*/
Location.query();

Location.query({groupId: $routeParams.groupId});
});

/* Services */
app.service("Location", function($resource, $routeParams) {
return $resource("/api/:groupId/location", {}, {
query: {method: "GET", isArray: true}
});
});

最佳答案

我认为你可以这样做:

 app.service("Location", function($resource, $routeParams) {
return $resource("/api/:groupId/location", {groupId:12345}, {
query: {method: "GET", isArray: true}
});
});

我还没有测试过,但我相信文档就是这样读的

关于javascript - AngularJS - 始终有一个参数作为资源的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12034350/

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