Exploring the concept of inheriting AngularJS modules

I am intrigued by the behavior of AngularJS. I am wondering if AngularJS modules inherit dependencies from other modules. Let's consider the following structure:

var PVNServices = angular.module('PVN.services', []);

PVNServices.factory('ItemService', ['$q', 'apiUrl', '$http', 'errorHandler', function($q, apiUrl, $http, errorHandler) {
    return {
        getAlert: function(alert_id, user_id, category_id) {
            return $http({  method: 'get',
                            url: apiUrl + 'getAlert/',
                            params : {
                'alert_id' : alert_id,
                'user_id' : user_id,
                'category_id' : category_id,
              }
            }).then(function(res){return res.result }, errorHandler);
        }
  }
}]);

var PVNControllers = angular.module('PVN.controllers', ['PVN.services']);

PVNControllers.controller('AppController', ['$scope', 'ItemService', function($scope, ItemService) {
  $scope.getAlert = function(alert_id, user_id, category_id) {
     ItemService.getAlert(alert_id, user_id, category_id).then(function(alert){
         $scope.alert = alert;
     }
  }
}]);

var PVNDashboard = angular.module('PVN', ['ngSanitize','ngMaterial','PVN.controllers'], function($interpolateProvider) {
    $interpolateProvider.startSymbol('<<');
    $interpolateProvider.endSymbol('>>');
});

PVNDashboard.run(function() {
    moment.locale('nl');
});

<body class="" ng-app="PVN">
</body>

In this setup, can I utilize the ItemService in the PVNDashboard module because it has controllers as a dependency, which in turn depends on services? Also, due to the ng-app being set to the PVN module, will the configuration of the PVN module (such as setting moment.js locale in this case) persist in the services because it is the first to run?

Answer №1

Indeed, all dependencies inherit in the system. The initial idea was to allow for the creation of a module for each specific feature and then combine them to build applications through module injections.

However, there is a caveat: Angular appears to utilize a unified namespace for all injectable components, potentially leading to overwriting of items with identical names. An example of this issue can be found on this blog post:

It remains uncertain if any updates have been made since then, although some feedback suggests that this particular problem may have been addressed in Angular 2.

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

How to toggle between two background colors in a webpage with the click of a button using JavaScript

I need help with a unique website feature. I want to implement a button that cycles between two different background colors (white and black) as well as changes the font color from black to white, and vice versa. My goal is to create a negative version of ...

Using Selenium in Java to interact with popup elements

Attempting to retrieve and interact with pop-up/alert elements using selenium in Java has been a bit challenging for me. Below is the code snippet I have been working on: import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import ...

Rzslider not functioning properly due to CSS issues

I am facing an issue where rzslider is not appearing in my app. However, when I copy the code to an online editor, it works perfectly fine. Below is the code snippet: var app = angular.module('rzSliderDemo', ['rzModule', 'ui.boo ...

Get a CSV file through EmberJs

I have been experimenting with various function calls, but I am unable to figure out how to initiate a CSV download in EmberJs. Below is the most recent code I have tried: let endpoint = '/api/foo/'; let options = { url: endpoint, type: ...

Facing delays in receiving responses for inner loop in node.js

Having an issue with retrieving data from the inner loop, as there is a delay in getting it. In my code, I am fetching the user list along with their ancestors from the SQL table. The ancestors are needed to verify their root values in the hierarchy/tree ...

The combination of Node.js module.exports and shorthand ternary operators for conditional statements

Can you explain the purpose of this line 'undefined' != typeof User ? User : module.exports and why is everything enclosed within (function(){})? I am having trouble understanding its significance. This code snippet is extracted from a library f ...

Having trouble with JSONP cross-domain AJAX requests?

Despite reviewing numerous cross-domain ajax questions, I am still struggling to pinpoint the issue with my JSONP request. My goal is simple - to retrieve the contents of an external page cross domain using JSONP. However, Firefox continues to display the ...

Adjust the size of each link in the highchart network diagram

Is it feasible to individually set the length of each link in a network graph using highcharts? I am interested in creating a visualization where each user is displayed at varying distances from the main center user. I have been experimenting with the lin ...

Steps to resolve the Angular observable error

I am trying to remove the currently logged-in user using a filter method, but I encountered an error: Type 'Subscription' is missing the following properties from type 'Observable[]>': _isScalar, source, operator, lift, and 6 more. ...

Error message: "Unfortunately, the requested page cannot be found when attempting to reload

After successfully deploying my Angular app to Firebase and being able to access the sign-in page without any issues, I encountered an error upon reloading the page: The file does not exist and there was no index.html found in the current directory or 404 ...

How to Extract a URL from an Anchor Tag without an HREF Attribute Using Selenium

How can I make a link, which normally opens in a new window when clicked, open in the current window instead? The link does not have an href attribute, only an id and a class. For example: <a id="thisLink" class="linkOut">someLinkText</a> I r ...

How can I toggle the visibility of a div based on whether a variable is defined or not?

How can I display a specific div on my webpage only when certain variables in PHP pull out a specific result from a database? I attempted to use the code snippet below, but it's not working as expected. Can someone provide guidance on how to achieve ...

Ways to automatically refresh HTML table in Django framework

How can I dynamically update the search content under the hostname column in an HTML table? The search content needs to be updated every time, and the row number should increase according to the number of hostnames entered by the user. Below is my index.h ...

The Angular ng-model feature seems to be malfunctioning across multiple views

I have been working on developing a single page application using ng-view, following a tutorial from here. Everything seems to be working fine except for the ng-model. I have a form with an input text bound to ng-model and a button that triggers a function ...

JavaScript code to verify if a checkbox is selected

Having some trouble making a text box value change based on checkbox status. I've attached a function to the onclick event of the checkbox, but it's not quite working as expected. <div> MyCheckbox <input type="checkbox" id="Check1" name ...

Send data with AJAX and PHP without refreshing the page

I have implemented the "add to favorite" feature on my WordPress project using a <form> submission. Each time I click on the add to fav button, it works fine, but the page always reloads which is quite annoying. To tackle this issue, I decided to imp ...

Check if the browser is compatible with the FREEZE and RESUME lifecycle events

Is there a method to determine if a browser is capable of registering the freeze and resume lifecycle events? Even after checking with Modernizr, a reliable JS library, it appears that these events are not supported. Although extremely beneficial, Safari ...

adding items into an array with the help of angularjs

The current code is functioning correctly and displaying the desired output, but I now wish to save each string or entry in an array to prevent loss of entered data. Currently, it only works for one item, but I need it to support multiple items and store t ...

A guide on successfully handling errors while utilizing S3.getsignedurlpromise() in node.js

I am faced with a challenge in my project involving a node.js server that downloads images from an s3 bucket and serves them to a React client. The issue arises when the wrong file key is sent to the S3 client, as I want to handle this error gracefully by ...

The ng-click directive does not function properly with div elements within the Ionic framework

Hey there, so I've encountered a little issue with my form. I have a multi-step form that I set up using the ng-show property and a function to control which steps are displayed based on user interaction. Everything was going smoothly until I noticed ...