Angular nameless ng-app

How and why is it used <body ng-app>? How can we assign controllers, directives, etc. For this unnamed module. Also explain how this relates to manually loading the Angular app. Fiddle

+4
source share
3 answers

In fact, these are three separate issues, but I am happy to solve each.

  • How and why is it used?

Angular will not and will not be able to load the application correctly if there is no entry point to this application. According to the documentation , if the parameter is not passed, which calls the application instance, angular will try the auto-bootstrap application for you by traversing the DOM and using the first instance of the ngApp directive that it encounters.

Usually we want to place our entry point in an element <body>in order to cover all the possible DOM that we need without cluttering it with elements <head>such as loading scripts and css. However, if you automatically download the application, the recommended placement is on the HTML element.

  • How can we assign controllers, directives, etc. for this unnamed module?

, .. -, angular .

, angular DOM, . ( ), . bootstrap → , :

Angular DOMContentLoaded angular.js script , document.readyState "complete". angular ng- . ng-, angular :

  • , .
  • DOM, ng-app . DOM angular.


  • ?

, :). JSFiddle, , AngularJS . JSFiddle angular, , ( ), DOM 2.

+3

Per angularjs.org:

" ngApp - , .

, ng-app - , html Angular, {{ }} . , 1 + 1. , ng-controller div .

+1

ng-app="". , . .. . Angular {{ 1 + 1 }} , - . ng-app=" " - , .

https://jsfiddle.net/n3hygcnd/3/

+1
source

Source: https://habr.com/ru/post/1608755/