How to add dependencies in a module already initialized in Angularjs?

Asked

Viewed 195 times

1

I usually use Angularjs with a structure where I have a Javascript script for each page of my application.

Main dependencies and project settings I define in a file app.js, which is used on all pages.

Sort of like this:

// 'js/app.js'

angular.module('app', ['ngAnimate', 'ui.validate'])


// 'js/usuarios/listar.js'

angular.module('app').controller('UsuariosController', function () {
      // resto do código
})

That is, I always added to the existing module app a functionality according to the page I upload (I am not using SPA).

Therefore, I need to click on the module app a specific dependency, for a given script.

In the case of the script js/usuarios/editar.js need to add ngFileUpload.

I would not like to added to mine app.js default, since I will not upload files to all pages of my application, but only in that specific where I use js/usuarios/editar.js.

It is possible to add a dependency on an angular module that has already been initialized?

  • From what I’ve seen on some links and websites, you’ll have to declare in the general file even if you only use in a module/controller.

  • @wrong mutiny. I’ve managed to do this :p

  • So... you no longer have an excerpt of the code that allows you to do this?

  • @mutated if you want to do the please translate, help the community:

  • Translated, dear Wallace.

1 answer

2


Translation of this answer

I use the following method and it works well with me:

var app = angular.module("myApp", []);

angular.module("myApp").requires.push('ngResource');

If you need to put more than one dependency on the same module, you can extend the push arguments()

angular.module("myApp").requres.push('dep1', 'dep2', 'dep3');
  • Amended with your suggestions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.