Search code examples
angularjsamd

Angular + angularAMD, load a controller before bootstrapping the app


First of all, thanks for this great plugin! (to the author of angularAMD)

I have some troubles. I have all modules loaded with ngAMD but two are inside my index.html because are templates and I'm including this way:

 <div ng-include="'views/header.html'"></div>

header.html uses HeaderCtrl but I have no idea how load before angularAMD.bootstrap...

More code:

header.coffee that requires app

define ['app', 'bootstrap'], (app, bs) ->
  'use strict'

  app.controller 'HeaderCtrl', ($scope, $rootScope) ->
    $scope.searchText = "";

    $scope.updateSearch = -> 
      $rootScope.searchText = $scope.searchText;

app.coffee

define ['angular', 'angularAMD'], (angular, angularAMD) ->
  'use strict'

  app = angular.module 'testsApp', [
    'ngRoute'
    'localization'
    'restangular'
  ]

  angularAMD.bootstrap app
  app

After bootstrapping app, ng tries to solve ng-include but HeaderCtrl is not loaded! This happens only with CTRL+F5 on page, it's a matter of loading time. I have no idea on how solve this. Any hint?


Solution

  • The problem was angularAMD.bootstrap app position. I was bootstrapping before loading each dependencies.

    I moved angularAMD.bootsrapp call after dep loading and all works now:

    bootstrap.coffee (the application bootstrapper)

    define [
      'app'
      'jquery'
      '_'
      'angularAMD'
      'ctrl/header'
      'ctrl/menu']
    , (app, $, _, angularAMD) ->
    
      # bootstrapping all dependencies
      $.get "modules/modules.json", (deps) ->
        # converts all array items from DEP to module!DEP
        deps = _.map deps, (dep) ->
          "module!#{dep}"
    
        console.log JSON.stringify deps
        require deps, ->
          console.log "Dependencies loaded!"
          # I'll bootstrap angular only after each dependencies are loaded, this prevents many 'random' issues.
          angularAMD.bootstrap app
    

    I hope this can help someone :)