raju
raju

Reputation: 5008

How to create module in angularjs

In all the angularjs tutorials I am gone through. Modules are created as follows

var newApp = angular.module('articles', []); 

or var routerApp = angular.module('routerApp', ['ui.router']);

I started my project with meanjs boiler plate code and controller starts as follows

angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
  function($scope, $stateParams, $location, Authentication, Articles) {
    $scope.authentication = Authentication;
    .....
    .....

]);

When I change it to

var newApp = angular.module('articles',[]);
newApp.controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
  function($scope, $stateParams, $location, Authentication, Articles) {
    $scope.authentication = Authentication;
    .....
    .....

]);

All the routes of articles stops working. If I want to include a new component into the module, how do I do it. I want to add angularFileUpload in to my module.

Sample code in angularfileupload is

angular
    .module('app', ['angularFileUpload'])
    .controller('AppController', function($scope, FileUploader) {
        $scope.uploader = new FileUploader();
    });

How do I add ['angularFileUpload'] if the module is already registered? Edit:

articles.client.modules.js

'use strict';

// Use Applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('articles');

Upvotes: 0

Views: 1819

Answers (2)

New Dev
New Dev

Reputation: 49600

angular.module("MyModule", []) (with []) is a setter function, that is - it registers a module.

angular.module("MyModule") without [] is a getter function, it retrieves a previously registered module.

Calling a setter twice re-defines the module.

I'm not familiar with meanjs boilerplate, but in all likelihood when you used a setter, you have redefined the module and whatever controllers, services, config, etc... that were previously registered were overwritten.

All you need to do is change what you added to:

var newApp = angular.module("articles");

Example:

angular.module("M", []).controller("A", function(){}); // module M has controller A

angular.module("M").controller("B", function(){}); // module M has controllers A, B

var app = angular.module("M", []); // module M re-registered
app.controller("C", function(){}); // module M has controller C only

Upvotes: 2

vivek
vivek

Reputation: 71

// Create a new module
var myModule = angular.module('myModule', []);

// register a new service
myModule.value('appName', 'MyCoolApp');

// configure existing services inside initialization blocks.
myModule.config(['$locationProvider', function($locationProvider) {
  // Configure existing providers
  $locationProvider.hashPrefix('!');
}]);

Usage

angular.module(name, [requires], [configFn]);

Upvotes: 0

Related Questions