Using ng-app without a value

后端 未结 3 1580
时光取名叫无心
时光取名叫无心 2020-11-29 08:59

Usually, you see Angular initialized on a page with a directive like ng-app=\"myModule\", but it is valid to just use ng-app. However, as things l

3条回答
  •  余生分开走
    2020-11-29 09:35

    To be clear, ngApp directive in AngularJS allows you auto bootstrap an AngularJS application by designating the root element of the app in your body/html tag automatically via DOM initialization. You can also have more control over this initialization process by manually 'bootstrapping' your app via angular.bootstrap, as well as bootstrapping multiple AngularJS applications (as long as you do not nest them). The main separation is that you have to use one process or the other, meaning either ng-app or angular.bootstrap separately, which I hope is obvious because you would not want to override initializations.

    A more in depth look at this process can be seen here: AngularJS Developer Guide: Bootstrap

    Now what does this all mean in context to your question? ng app initialization allows only a single instance per page, this one module per a single HTML element; whereas angular.bootstrap gives you this functionality yet manually. The real magic would be if you could use ng-app like angular.bootstrap; so in that case you can look at ngModule.

    I would like to reference an article that helped me better understand how I can leverage ngModule like ngApp, however you should note that even if you use ngModule you're continuously leveraging functionality that mimics ngApp and extends it. Therefore you can conclude that the functionality that will remain will be DOM app initialization via X method... whether or not you get around using ngApp, you'll still have to deal with the big picture.

    Let me know if it helped, or at least pointed you in a direction you can follow.

提交回复
热议问题