Placement of the ng-app directive (html vs body)

前端 未结 3 1058
北海茫月
北海茫月 2020-12-04 08:05

I recently reviewed the code for a webapp built with angular and found that it was written with the ng-app=\"myModule\" directive placed on the

相关标签:
3条回答
  • 2020-12-04 08:34

    There is no big difference where you put ng-app.

    If you put it on <body> then you have a smaller scope for AngularJS which is slightly faster.

    But I have used ng-app on the <html> for manipulating the <title>.

    0 讨论(0)
  • 2020-12-04 08:51

    I was on a team working on a legacy app and found it best to use the ng-app tag in a div that is used as a wrapper to isolate new code from the legacy code.

    We discovered this while working on the app that was heavily relying on jqGrid and Dojo.

    When we added ng-app to the head tag it blew up the site, but when we used a wrapper we could use Angular with no problems.

    0 讨论(0)
  • 2020-12-04 08:54

    AngularJS will bootstrap the first ng-app it finds! That's it. If you have more than one ng-app, it will only process the first one. If you want to bootstrap any other element use angular.bootstrap()

    The value of the ng-app attribute is a module that have been created using:

    angular.module("module_name", [])
    

    A module defines how angular will bootstrapped because we do not have a main() method unlike other programming languages. If ng-app's value is empty, then it defaults to use 'ng', the default module.

    It was said to be slightly faster because angular will process all of the elements inside the element where ng-app was. But I doubt slightly part because the difference will be hardly noticeable at all, unless you have a very very bulky DOM.

    If you want examples here: http://noypi-linux.blogspot.com/2014/07/angularjs-tutorials-understanding.html

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