1

I have two files

test1.js

angular.module("test1",[]).controller("test1Ctrl",function(){
// some code
})

and

test2.js

angular.module("test2",[]).controller("test2Ctrl",function(){
//some code
})

app.js

var app = angular.module("testApp",['test1','test2']);

My question is how I can inject my both controller in app.js from test1.js and test2.js using same module and not using module test1 and test2?

4
  • 1
    What's wrong with the code in your question? There's nothing wrong with using independent modules in your app. In fact, I highly recommend it Commented Jun 24, 2015 at 4:40
  • @Phil Thanks. But is there any way to do it? Commented Jun 24, 2015 at 4:50
  • Do u mean : Using a controller from different application? If yes - you can always use 2 ng-apps in HTML together. and also you can use same ng-app at 2 different places Commented Jun 24, 2015 at 4:57
  • You can also look for ng-directive. As said controllers are special directives. Commented Jun 24, 2015 at 4:59

1 Answer 1

2

If you want to keep controllers in separate file then,

app.js

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

test1.js

app.controller("test1Ctrl",function(){
// some code
})

test2.js

app.controller("test2Ctrl",function(){
// some code
})

Finally include js files in html,

<script src="app.js"></script>
<script src="test1.js"></script>
<script src="test2.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.