Is it possible to pass variables into a factory function?

I have a factory that is a wrapped $resource object and I am trying to include a custom header for http authentication. However, I am struggling to figure out how to pass data into the header.

app.factory('SomeFactory',['$resource', function($resource){
    return $resource('https://third.party/:token',{token: access_token},{
    get:{
        method:'GET',
        header:{
        'some varialbe': my_var //I would like to pass this variable
     }
    }
    });
}])

Answer №1

app.factory('CustomFactory', ['$resource', function($resource){
  return function(token, custom_var){ // parameters to pass
    return $resource('http://third.party/:token',{
      token: token
    },{
      get: {
        method: 'GET',
        header: {
          'custom variable': custom_var
        }
      }
    });
  };
})

...or a similar approach.

app.controller('CustomController', ['CustomFactory', function(customFactory){
  /* ... */
  $scope.customModel.result = customFactory('accesstoken','myvar')
  /* ... */
});

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

Selecting objects in Three.js using the camera but without using the mouse

I am working on a Three.js app where I need to determine the object that the perspective camera is focusing on. In order to achieve this, I consulted the raycaster documentation. Most of the resources I came across discuss using raycasting with a camera an ...

What is the process for refreshing the component content in Angular 2?

There was a moment during execution when my URL had the following appearance: http://localhost:4200/personal/51c50594-a95c-4a18-ac7b-b0521d67af96 I desire to visit a page with a different GUID and different content. http://localhost:4200/personal/{other ...

Showing arbitrary text on Vue.js template

In my Vue.js application, I have a Loader component that randomly displays one of several messages. Here is how I implemented it: Vue.component('Loader', { data() { const textEntries = [ 'Just a moment', ...

Event handler or callback function in Socialite.js

Exploring the capabilities of Socialite.js for the first time has been quite intriguing. This JavaScript plugin allows loading social media plugins after page load, adding an interesting dynamic to website interactivity. However, I am faced with the challe ...

The functionality of jPanelMenu is interfering with the ng-click event in AngularJS

In my ToDo application, I am utilizing the jPanelMenu plugin for the left side menu. To implement this functionality, I have developed a directive that applies jPanelMenu to the necessary elements. While everything is functioning as intended, I have encou ...

Steps for redirecting from a URL containing location.hash to a different page

Recently, I decided to move a section of one of my webpages from dummy.com/get-started/#setup to its own page at dummy.com/setup. After making this change, I went ahead and deleted the old /get-started page on my website. Many people have bookmarks saved ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Angular: Design dependent on attributes

Can I customize the styling of a div in accordance with a boolean property called "isActive" on my controller using Angular? <div class="col-md-3" (click)="isActive = !isActive"> <div class="center"> <i class="fa fa-calendar"& ...

Error Encountered: unable to perform function on empty array

I've encountered an issue with my Vue JS 2.6.10 application after updating all packages via npm. Strangely, the app works perfectly fine in development environment but fails to function in production. The error message displayed is: Uncaught TypeErr ...

How to pass variables in AngularJS

When displaying data in a grid, I need to change the button icon on click of the active or inactive button. The functionality is working well, but I am having trouble finding the clicked active button to change its icon. In jQuery, we can use "this", but ...

Having trouble with a beginner problem that's hindering the functionality of my code

I have been struggling with a particular piece of code and it is driving me crazy as I am unable to locate the source of my error: $.post($form.attr('action'), $form.serialize(), function (result) { console.log(result); if (result.succes ...

Avoid triggering a second event: click versus changing the URL hash

One of the pages on my website has tabs that load dynamic content: HTML <ul> <li><a href="#tab-1">TAB 1</li> <li><a href="#tab-2">TAB 2</li> <li><a href="#tab-3">TAB 3</li> </ul&g ...

The ajaxStart event does not seem to be triggering when clicked on

I am having trouble adding a loader to my site using the ajaxStart and ajaxStop requests to show and hide a div. The issue is that these requests are not being triggered by button onclick events. <style> // CSS for loader // Another class with o ...

Is it possible to ensure that an asynchronous function runs before the main functional component in React js?

My challenge involves extracting data from an API by manipulating a URL. Specifically, I must retrieve a specific piece of information upon page load and then incorporate it into my URL before fetching the data. var genre_id; var genre; const MOVIE_URL = ` ...

What is the best way to divide my JavaScript objects among several files?

Currently, I'm in the process of organizing my JavaScript code into separate libraries. Within the net top-level-domain, I manage two companies - net.foxbomb and net.matogen. var net = { foxbomb : { 'MyObject' : function() { ...

Curious about the method of refining a list based on another list?

As a beginner in coding, I am facing a challenge in my code.org assignment. I need to filter songs by a specific artist from a dataset. Currently, I have code that randomly selects an artist, but I am struggling to filter out songs by that artist from the ...

Is there a way to create a header that fades out or disappears when scrolling down and reappears when scrolling up?

After spending some time researching and following tutorials, I have not made much progress with my goal. The task at hand is to hide the top header of my website when the user scrolls down and then make it reappear when they scroll back up to the top of t ...

Solving the error message: "Objects cannot be used as a React child (found: object with keys {})"

I am currently in the process of developing a full stack website that utilizes React, Express, Sequelize, and mySQL. The site is operational with features like registration and login implemented successfully. However, I encountered an issue when trying to ...

Manipulator - Unveiling Discord User Profile Popup

Hey there, I'm currently working on a web project and I want to send messages in a Discord server using Puppeteer without relying on the Discord.js library. While I have successfully set up user authentication and navigated to the correct chat room, I ...

Exploring the application of the Angular Controller and how it interfaces with the controllerAs

When trying to learn Angular, I often come across articles that leave me puzzled. One particular aspect that has me stuck is the significance of keywords Controller and controllerAs in directives. I found the code snippet on this website: app.controller ...