Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS module dependencies - clarification?

I've been investigating the tutorial example from AngularJs's site ( this one)

(The main html is pretty empty (except for ng-view and ng-app=phonecatApp))

The app.js file includes this :

var phonecatApp = angular.module('phonecatApp', [
  'ngRoute',
  'phonecatControllers',
  'phonecatFilters',
  'phonecatServices'
]);

phonecatApp.config(['$routeProvider',...

Ok, so we have phonecatApp module with many dependencies.

But then I saw the controller.js file ( they opened a new module for the controllers)

/*1*/   var phonecatControllers = angular.module('phonecatControllers', []);
/*2*/   
/*3*/   phonecatControllers.controller('PhoneDetailCtrl', ['$scope', '$routeParams', 'Phone',
/*4*/     function($scope, $routeParams, Phone) {
/*5*/       ...
/*6*/       });
/*7*/   
/*8*/     }]);

Phone is a service. ( which is on another module , different js file)

Question

In line #3 , how does it know what is the Phone parameter ? they didnt add any dependecy module in line #1 !
Same for the $routeParams , how does it know it ? they didn't add any dependency in line #1 to the ngRoute !

Am I missing something here ?

like image 685
Royi Namir Avatar asked Feb 22 '14 14:02

Royi Namir


People also ask

What is dependency in AngularJS?

Dependency Injection in AngularJS can be defines as the software design pattern which defines the way the software components are dependent on each other. AngularJS provides a set of components that can be injected in the form of dependencies such as factory, value, constant, service, and provider. factory.

CAN modules have dependencies?

Modules can depend on SDKs, JAR files (libraries) or other modules within a project. When you compile or run your code, the list of module dependencies is used to form the classpath for the compiler or the JVM.

What are modules in AngularJS?

An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module.

Can we have multiple modules in AngularJS?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.


2 Answers

This citation from Pawel Kozlowski' book seems to be relevant:

A service defined in one of the application's modules is visible to all the other modules. In other words, hierarchy of modules doesn't influence services' visibility to other modules. When AngularJS bootstraps an application, it combines all the services defined across all the modules into one application, that is, global namespace.

like image 163
Ivan Chernykh Avatar answered Oct 25 '22 05:10

Ivan Chernykh


The dependency injection works by default for every component of Angular.

This is because every component is defined inside the angular object, so it can track it all.

You can check this http://docs.angularjs.org/guide/di out to understand how it works.

like image 34
Ivan Guardado Avatar answered Oct 25 '22 04:10

Ivan Guardado