AngularJS - make it a practice to set a default parameter for every Resource

I have encountered a situation where the API I am working with requires a :groupId parameter in the URL for many operations. Is there a simple way to eliminate the need to pass $routeParams.groupId in every function call?

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; /* Can this be avoided? I want to make it accessible to Location without explicitly passing it*/
  
  Location.query();
  Location.query({groupId: $routeParams.groupId});
});

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

Answer №1

Here's a possible solution:

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

Although not tested, it seems to align with the documentation

Answer №2

In my case, I found that the previous solution did not solve my issue, so I decided to add .query at the end of the function call.

app.service("Location", function($resource, $routeParams) {
    return $resource("/api/:groupId/location", {groupId:'@groupId'})
                                                  .query({ groupId: 12345 });
 });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Additional non-essential attributes (e.g. title) were provided to the component but could not be utilized

runtime-core.esm-bundler.js?d2dd:38 [Vue warn]: The component received extraneous non-props attributes (title), which could not be automatically inherited due to rendering fragment or text root nodes. at <ProductTable title="Product List" & ...

Re-Rendering Component in React Continuously Keeps Checkbox Checked Event Flowing

I am working on a material ui checkbox formgroup that is generated dynamically based on data received from an API. Essentially, the user is presented with a question and a set of answers. The user checks all the valid answers and clicks 'next'. I ...

Javascript Code. Can you explain what this means: ", (err) => { }"?

I'm a bit puzzled as to how to describe this, so I'm struggling to articulate my question clearly. Can someone please explain what the operator is and what it is accomplishing in this code snippet? My assumption is that it's passing a functi ...

When the text for the Rails confirmation popup is sourced from a controller variable, it may not display properly

Attempting to customize my submit_tag confirmation popup within my Rails view, I encounter an issue when trying to utilize an instance variable from the Rails controller. Within the controller, I set up the variable as follows: @confirmation_msg = "test" ...

Displaying JSON data in HTML using Angular

If you have a straightforward controller: controller: function($scope, $http){ $http.get(templateSource+'/object.customer') .then(function(result){ $scope = result.data; }); } The content of my object.customer file is a ...

Following the HTTP POST response, it is not possible to modify the headers once they have already been sent to the

My apologies for any typos, as I am from Brazil I checked the question mentioned below, but unfortunately it did not provide a solution to my issue ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client I have a method that I execut ...

Loading Images in Advance with jQuery, Native JavaScript, and CSS

After successfully implementing a method to hover on a small image and load an additional image to the right side of the page, I am now looking to enhance user experience by preloading images. Is there a way to preload an image using JQuery, allowing it t ...

Comparing front end automation between JavaScript and Java or Ruby

Could you provide some insights on why utilizing a JS framework like webdriverio is preferred for front end automation over using Selenium with popular languages like Java or Ruby? I understand that webdriverio and JS employ an asynchronous approach to fr ...

Is it important that the end date does not exceed the start date?

In my request, the end date cannot be later than the start date. For example, if the start date is 24/4/2017 and the end date is 23/4/2017, the search function should be disabled for the calendar date 23/4/2017. Only dates from 24/4/2017 onwards should be ...

Is it possible to generate dynamic HTML based on the parameters in a GET request

Imagine you have a RESTful web server and want to dynamically load HTML based on the incoming request. For instance, if an HTTP GET request for /user/123 is received by the browser, you would like to display an HTML page specifically tailored for User #12 ...

The data retrieved from the server is not appearing in the Select2 dropdown

This is a script I wrote using Ajax to display search results in a select2 dropdown. However, I am facing an issue where the server response is not appearing in the select2 dropdown. $('#txtTest').select2({ minimumInputLength: 2, ajax: { ...

Issues with Bootstrap tabs loading jQuery and Ajax are preventing the proper functionality

I am currently facing an issue with a tab pane designed using Bootstrap. The problem arises when attempting to load external pages into the tabs through Ajax using a jQuery script. While the ajax script successfully loads the content of the pages, I am en ...

How can you enable fullscreen for a featherlight iFrame?

I have implemented Featherlight to display an iframe within a popup modal on my website. If you click the iframe button, you can see a demo of how it works. One issue I am facing is that the generated iframe tag by Featherlight does not include allowfulls ...

Algorithm that continuously increases a given date by 3 months until it surpasses or matches the specified creation date

I am currently working with QuickBase, a platform that involves HTML. My goal is to develop a code that can take a [fixed date] (always in the past) and increment it by 3 months until the MM/YYYY exceeds or equals the MM/YYYY of the [creation date]. Once t ...

Place attribute value directly under the border of an HTML element using a combination of JavaScript and CSS

I'm currently working on a JavaScript script to scan the DOM for elements that have a specific custom attribute called example-type. The goal is to apply CSS styling to draw a border around these elements and then display the value of the example-type ...

altering elements with AngularJS depending on dropdown selection

I am incorporating AngularJS into my project There are 3 select dropdowns and a button that fetches a JSON list. The primary select dropdown is constructed as follows: <select id='sort' ng-model='sort'> <option value=&apos ...

Making HTTP requests with axios in Node.js based on multiple conditions

I'm facing an issue with making get calls using axios to both activeURl and inactiveURl. The goal is to handle error messages from the activeUrl call by checking data from the inactiveUrl. However, I keep receiving error messages for the inactiveURL e ...

Split a string into chunks at every odd and even index

I have limited knowledge of javascript. When given the string "3005600008000", I need to devise a method that multiplies all the digits in the odd-numbered positions by 2 and those in the even-numbered positions by 1. This code snippet I drafted seems to ...

Tips for positioning a picklist field dropdown on top of a lightning card in Lightning Web Components

Attempting to resolve an issue with CSS, I have tried adding the following code: .table-responsive, .dataTables_scrollBody { overflow: visible !important; } However, the solution did not work as expected. Interestingly, when applying a dropdown pickli ...

Guide to integrating jssor into an AngularJS application

Struggling to integrate jssor with angularjs, it seems to be failing because jssor is being initialized before angularjs, causing the ng-repeat elements to not resolve correctly. <div id="slider1_container"> <div u="slides"> <!-- Th ...