Creating custom services that modify or extend default services in a shared module

I am facing a dilemma with my shared module in AngularJS 1.6.5. This module is designed to be used across multiple applications, each requiring different services within the module to be overridden to cater to their specific needs. These variations are mainly related to request authorization methods for different REST endpoints used by the host applications.

The common approach of defining the shared module first and then creating services with similar names in individual applications seems messy and inefficient. Especially considering that each application already has its own set of similar services under different names.

Do you have any suggestions on how to tackle this challenge effectively?

Just to clarify:

In my shared module, there may be directives with controllers that depend on a service

class MyDirectiveController {
  /*@ngInject*/
  constructor($element, entity) {
    this.$element = $element;
    this.entity = entity;
  }
  
  $onInit() {
    this.entity.get(this.id)
      .then((data) => this.dataSet = data);
  }
}

Within the shared module, there is a placeholder service:

class Entity {
  /*@ngInject*/
  constructor($q) {
    this.$q = $q;
  }
  
  get() {
    console.info('You must include your own override service for retrieving entity data');
    return this.$q.reject();
  }
}

My goal is to somehow replace this dummy service in the shared module with a custom service from the parent application:

entity = MyAppEntityService;

Answer №1

During the configuration phase, the sequence of service definitions is crucial. However, during runtime, the module hierarchy takes precedence, unless there are conflicting definitions within a single module.

If a shared module contains a service called foo, any subsequent modules can override this service when loaded:

angular.module('app', ['shared']).factory('foo', ...);

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

Tips on pausing until another function completes using async functions

Is there a way to ensure that my Angular functions are executed sequentially in the ngOnInit() lifecycle hook? I attempted to use async, but it didn't work as expected... nbPage() getAllCommerces(page) sort() getCommerces(isFirstLoad, event) import ...

Adjust the width of the table after refreshing it for 10 seconds when resizing the column?

I am facing an issue with a page that refreshes every 10 seconds and has a resizable column feature. However, after the page refresh, the table header (th) reverts back to its original position. The main question is how to obtain the width of a specific ...

What are some tips for integrating filepicker in Angular applications?

Having some trouble using angular with filepicker. The documentation is a bit vague. I was able to get it working when placed in the main html, but as soon as I tried putting it inside a template, it stopped functioning. Can anyone offer assistance? ...

Tips for exporting telegram information to a Google spreadsheet

Recently, I set up a basic telegram bot using the telegraf framework and wrote this code snippet to log essential data: bot.on('text', (ctx, next) => { console.log(`[text] ${ ctx.message.chat.id } ${ ctx.from.username } ${ ctx.message.chat.f ...

Receiving JSON dynamically (using socket.io) may result in parsing issues

I am currently working with JSON data that is correctly formatted at 100% accuracy. My issue arises when I execute the following code successfully: var data = {"datas":[{"matts":{"active":"1","status":"off"},"config":null,"adapters":[]}}]}; console.dir( ...

A guide on retrieving information from a JSON file using AngularJS 2

As a beginner in AngularJS2, I am looking to extract information from a JSON file and store it in an array. This will allow me to utilize the JSON data within my components. Any assistance on this matter would be greatly appreciated. ...

Getting a page element by its id with QWebEngineView is a simple task that can be

Is there a way to access the page ElementById in order to input a value? import sys from PyQt5 import QtWebEngineWidgets from PyQt5.QtCore import * from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import * from PyQt5.QtWidgets import QAction from PyQt ...

The dropdown in the UIB Typehead is not displaying

I am facing an issue with the UI-Bootstrap typeahead where the dropdown does not display when the data is received. $scope.obtainUsers = function (valor) { console.log(valor) $http({ method: "POST", url ...

Troubleshooting Event Tracking Problems with Brave Browser on PostHog

After successfully implementing Posthog with React and testing it on Chrome and Firefox, we encountered issues when trying to test it on Brave/Microsoft Edge Browsers. It appears that the default ad blocker feature in these browsers is causing the problem. ...

What is an example scenario where Async Storage can be tested using Jest-expo?

To better understand the testing of Mock-async-storage for reactjs, I decided to replicate an example. If you have any suggestions on a different approach to testing, please feel free to share. I attempted to mimic a use case illustrated on this stack over ...

Adjusting the size of a canvas element based on changes in the browser window dimensions

I'm currently working on developing a website layout in React that consists of: A top bar A right side bar A canvas element that occupies the remainder of the screen space These elements need to adjust to the browser window size changes while fillin ...

Persisted state in Vuex fails to retain data after the page is refreshed

I recently added persisted state to my Vue application using the command npm install --save vuex-persistedstate. After that, I configured my Vuex store.js file in the following way: import Vue from 'vue' import Vuex from 'vuex' import ...

Exploring the possibilities of node-webkit: node-odbc encounters a setback

Currently, I'm in the process of developing a desktop application utilizing node-webkit. The main functionality of the app involves querying an Oracle database. To establish the connection with the database, I have integrated node-odbc. To ensure tha ...

Incorporate JSON data from a file into a d3 visualization within a Node.js/Express

I am working on an express app and I have the requirement to load JSON data from the "public" folder into d3 (version 4). Here is my current folder structure: public |-myData.json view |-index.jade app.js The JSON data I want to load using d3: { { ...

Updating data in parent or child controllers is not being reflected in the resolved object post-editing

One way I am sharing data between parent and child controllers is by utilizing the resolve option provided in ui-router. Below is a snippet of the code: var app = angular.module('app',['ngRoute','ui.router']); app.config(fun ...

The mysterious occurrence of "Undefined" popping up in the URL of my ASP.NET application, could this be somehow linked to Google?

Several users are encountering a perplexing issue while using my web application. During their usage, they click on a button or link that redirects them to another page, but encounter a "page not found" error with a URL like: undefined I initially suspe ...

Sequelize throws an error stating 'Model is not defined' when trying to establish a relationship across multiple databases

I am in the process of creating a separate sequelize instance for each database. In some cases, tables in DB1 have relationships with tables in DB2, and vice versa. One such relationship is DB1.Utilisateur.contenuProvenance_id => DB2.Contenu.id and DB2. ...

Rendering based on conditions with a pair of values

I am trying to render my component only if the id is equal to either 15 or 12. My current approach is not working as expected, it only renders the component when I check for one id at a time, but I need to check for both. {query_estate_id === 15 || q ...

Dealing with delays in Protractor

After spending a considerable amount of time developing automated tests in Protractor, I have encountered situations where the only solution seems to be using the browser.sleep() function. While it is not my preferred method, sometimes it becomes necessary ...

Displaying currency in Vue components

I'm having trouble maintaining the decimal format when trying to input currency values using numeraljs and plain javascript. I can't seem to find a solution to this issue. Below is my code snippet: <input v-model="amountValue">< ...