Why do I need to angular.bootstrap even when I declare ng-app=“MyApp” in JSFiddle

后端 未结 2 885
悲&欢浪女
悲&欢浪女 2020-12-06 03:12

I do not truly understand why it is necessary to do an angular.bootsrap document, [\'MyApp\'] at the end of my CoffeeScript code that manages the module and con

相关标签:
2条回答
  • 2020-12-06 03:53

    Tl;dr

    Set the second drop-down in jsFiddle to "No wrap - in <head>" and you won't need angular.bootstrap line.

    FIDDLE

    Explanation

    When Angular library is loaded it will scan the DOM looking for element with ng-app directive. When it finds one it will begin the bootstrapping proces.

    In that process Angular will take the value of ng-app attribute (in your case that's InventoryModule) and will try to find an angular module with the same name. If it fails it will throw: Uncaught Error: No module: <module name>.

    In your fiddle you have set the "Code Wrap" select box to "onLoad". This drop-down instructs jsFiddle when to initialize the JS code that you've put in JS frame. When it's set to "onLoad", the code will run in onLoad window event.

    On the other hand, Angular bootstrapping process will run on $(document).ready(), and because $().ready event is fired before "onLoad" event, Angular will try to init the InventoryModule module before the module is even defined, and that's where the dreaded "No module" error will be thrown.

    angular.bootstrap() is a manual way of doing the same thing that Angular already does in it's $().ready() handler.

    0 讨论(0)
  • 2020-12-06 04:05

    Take a look at the error console. Your code throws an exception:

    Uncaught Error: No module: InventoryModule
    

    I think it has something to do with it. Manually bootstrapping by calling angular.bootstrap seems to workaround the actual problem.

    0 讨论(0)
提交回复
热议问题