nguyên
nguyên

Reputation: 5316

angularjs - one module define in multi place

I have problem when define angularjs module in multi place

angular.module('ty', [])
.directive('v1', function() {
      return{
        restrict: "E",      
        template: '<div>1</div>'
      };
});

angular.module('ty', [])
.directive('v2', function() {
      return{
        restrict: "E",      
        template: '<div>2</div>'
      };
})

This problem can solve by define a variable like this

var a = angular.module('ty', []);
a.directive('v1', function() { ...
a.directive('v2', function() { ...

http://jsfiddle.net/66Je2/1/

My question is: Can we define module in multi place without global variable ?

Upvotes: 0

Views: 37

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

Quoted from the docs:

Beware that using angular.module('myModule', []) will create the module myModule and overwrite any existing module named myModule. Use angular.module('myModule') to retrieve an existing module.

In your case, your are re-creating your module again and removing the previous one.

Upvotes: 3

Related Questions