简体   繁体   English

ng-app指令的作用是什么?

[英]What is the role of ng-app directive?

I am new to angularJS. 我是angularJS的新手。 Now I am trying to write a simple angular application. 现在我正在尝试编写一个简单的角度应用程序。 But I am wondering the role of the ng-app directive. 但我想知道ng-app指令的作用。

<html ng-app>

And is it possible to add ng-app in a div tag instead of html tag? 是否可以在div标签中添加ng-app而不是html标签? If possible, is there any differences between <html ng-app> and <div ng-app> , for example, the operating efficiency ? 如果可能, <html ng-app><div ng-app>之间是否有任何差异,例如,运营效率?

The ng-app directive designates the root element of the application and is typically placed near the root element of the page - eg on the <body> or <html> tags.You can only have one ng-app directive in your HTML document.It is also used to load various AngularJS modules in AngularJS Application. ng-app指令指定ng-app的根元素,通常放在页面的根元素附近 - 例如在<body><html>标记上。您的HTML文档中只能有一个ng-app指令。它还用于在AngularJS Application中加载各种AngularJS模块。 The AngularJS framework will only process the DOM elements and its child elements where the ng-app directive is applied AngularJS框架将仅处理ng-app指令的DOM元素及其子元素

Usage 用法

<div ng-app="" ng-controller="myCtrl">
</div>

OR 要么

<div ng-app="myApp" ng-controller="myCtrl">
</div>

var app = angular.module('myApp', []);

The ng-app directive is for bootstrapping your application. ng-app指令用于引导您的应用程序。

The element with ng-app is the root element : it wraps the other directives of your application. 使用ng-app元素根元素 :它包装应用程序的其他指令。

<div ng-app="myApp">
    <div controller="ctrl"></div> <!-- working -->
</div>
<div controller="myOtherCtrl"></div> <!-- not working -->

According to the documentation : 根据文件

Directive Usage 指令用法

as attribute: 作为属性:

<ANY ng-app="angular.Module" [ng-strict-di="boolean"]> ... </ANY>

So you can add ng-app on a div or any other element. 因此,您可以在div或任何其他元素上添加ng-app

The ng-app attribute represents an Angular directive named ngApp (Angular uses spinal-case for its custom attributes and camelCase for the corresponding directives which implement them). ng-app属性表示一个名为ngApp的Angular指令(Angular为其自定义属性使用spinal-case为实现它们的相应指令使用camelCase )。 This directive is used to flag the html element that Angular should consider to be the root element of our application. 该指令用于标记Angular应该认为是我们应用程序的根元素的html元素。 This gives application developers the freedom to tell Angular if the entire html page or only a portion of it should be treated as the Angular application. 这使应用程序开发人员可以自由地告诉Angular整个html页面或其中只有一部分应该被视为Angular应用程序。

Thus, you can add ng-app in the div tag. 因此,您可以在div标签中添加ng-app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM