marcoslin / angularAMD

Facilitate use of RequireJS in AngularJS
http://marcoslin.github.io/angularAMD
MIT License
734 stars 171 forks source link

Loading modules #118

Closed igor-olshevsky closed 9 years ago

igor-olshevsky commented 9 years ago

Hi! In documentation I didn't found how to use controllers from other modules. For example I've basic module 'app' and I need use controller from another module 'common'. How can I do this? Thanks.

marcoslin commented 9 years ago

The easiest way to do this is simply to load the common packages as a dependency to your app.js and add the needed reference to your app creation:

define(['angularAMD', 'common'], function (angularAMD) {
    var app = angular.module('webapp', ['common.package']);
    ...
})

You can also try to lazy load the common package using ngload.

If that doesn't answer your question, please provide some more detail about your app and this common package.

igor-olshevsky commented 9 years ago

Have error: https://docs.angularjs.org/error/$injector/modulerr?p0=viewer_spa&p1=undefined. app.coffee: define(['angularAMD', 'common', 'angular-route', 'ui-bootstrap', 'translate', 'calendar-ui', 'common/capitalize'], (angularAMD, common) -> module = 'viewer' common = 'common' app = angular.module("viewer_spa", ['ngRoute', 'ui.bootstrap', 'ui.calendar', 'common'])

app.config(($routeProvider) -> $routeProvider.when "/", angularAMD.route templateUrl: '/spa/' + module + '/views/application/home.html', controller: 'HomeCtrl', controllerUrl: 'controllers/application/home_controller'

$routeProvider.when "/profile",
  templateUrl: '/spa/' + common + '/views/profile/profile_page.html',
  controller: 'common.ProfileCtrl'

.otherwise
    redirectTo: '/'

)

angularAMD.bootstrap(app); )

common.coffee: define(['ngload'], () -> angular.module('common', ['restangular']) );

igor-olshevsky commented 9 years ago

Can you show me example how to split controllers on modules using your AngularAmd, 'cause I lost 3 hours and didn't understand how to do this... Thanks for help!

igor-olshevsky commented 9 years ago

https://github.com/igor-olshevsky/amd_example

marcoslin commented 9 years ago

I am not sure what you mean by "split controllers on modules". In your comon/profile/profile_page, you are using app.controller to define a controller. In this case, you can just load it using controller and controllerUrl syntax via angularAMD.route.

Also, when you define controller using app.controller, you do not need to return anything as you will be using dependency injection to use that controller.

Take a look at: https://github.com/marcoslin/angularAMD-sample/

igor-olshevsky commented 9 years ago

Ok, if a want to move profile page to the common.package, how can I use it in app.js(.coffee)? I've confused, sorry)

marcoslin commented 9 years ago

If you are defining using syntax of app.controller, you can just move the page and update dependencies.

You need to setup something simple to start with. The github project you setup wont run for me because it has not /package in it.

Start with something simple such as: https://gist.github.com/marcoslin/df4b741e92b2829eeae8

It is still not clear to me what are you trying to accomplish.

marcoslin commented 9 years ago

Closing for now due to lack of response from user. Please reopen with detail if needed.

igor-olshevsky commented 9 years ago

Thanks, I already understand how I can do this.