AngularJS seed: putting JavaScript into separate files (app.js, controllers.js, directives.js, filters.js, services.js)

ChrisDevo提出了一个问题:AngularJS seed: putting JavaScript into separate files (app.js, controllers.js, directives.js, filters.js, services.js),或许与您遇到的问题类似。

回答者Danger14Justin L.给出了该问题的处理方式:

The problem is caused from you "redeclaring" your application module in all your separate files.

This is what the app module declaration (not sure declaration is the right term) looks like:

angular.module('myApp', []).controller( //...

This is what assignment (not sure if assignment is the right term either) to your application module looks like:

angular.module('myApp').controller( //...

Notice the lack of square brackets.

So, the former version, one with the square brackets, should only be used once, usually in your app.js or main.js. All other associated files — controllers, directives, filters … — should use the latter version, the one without the square brackets.

I hope that makes sense. Cheers!

希望本文对你有帮助,欢迎支持JavaScript中文网


公众号

欢迎 JavaScript 爱好者