Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Yehuda Menahem
Yehuda Menahem
5,402 Points

AngularJs - dependencies

Hi, I have an app with 3 files: app.js controller.js directive.js

in the app.js I have the declaration of the app and I want the directive.js will have dependency on the controller.js. can someone tell me how to do it?

Thank you, Yehuda

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

Attach them to the same module:

angular.module("moduleName", [])

// controller .js
angular.module("moduleName")
   .controller( // ... )

// directive.js
angular.module("moduleName")
   .directive( // ... )
Yehuda Menahem
Yehuda Menahem
5,402 Points

I tried that, and the thing is it - I need the directive to be dependent on the controller. for example if I wont have the app js, and I will define the app on the controller, it will look like that:

// controller .js angular.module("moduleName", ["appDependency"]) .controller( // ... )

// directive.js angular.module("appDependency",[]) .directive( // ... )

but I want the app will be define in app.js, and something goes wrong when I try all together (?)

Seth Kroger
Seth Kroger
56,413 Points

If your directive is dependent on the controller then it should be the other way around:

// controller.js 
angular.module("appDependency",[]) .controller( // ... )

// directive.js 
angular.module("moduleName", ["appDependency"]) .directive( // ... )

Also make sure your scripts are included in the proper order.