加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS ng-app的自动加载与属性值

发布时间:2020-12-17 09:55:24 所属栏目:安全 来源:网络整理
导读:ng-app 指令用于告诉 AngularJS 应用当前这个元素是根元素,所有 AngularJS 应用都必须要要一个根元素。 使用ng-app来标记一个DOM结点,在网页加载完毕时会 自动引导 (自动初始化)应用程序。 ng-app可以带属性值,可以指定加载应用模块的名称,ng-app="模


ng-app 指令用于告诉 AngularJS 应用当前这个元素是根元素,所有 AngularJS 应用都必须要要一个根元素。


使用ng-app来标记一个DOM结点,在网页加载完毕时会自动引导(自动初始化)应用程序。

ng-app可以带属性值,可以指定加载应用模块的名称,ng-app="模块名称"。


但是HTML文档中只允许有一个 ng-app 指令,如果有多个 ng-app 指令,则只有第一个会被使用。

所以想要实现自动加载,那么就不能让ng-app带有属性值,即不能指定载入应用模块名称。


正确写法

<bodyng-app>
<div><p>{{5+5}}</p></div>
<div><p>{{10+10}}</p></div>
</body>


只加载第一个块

<body>
<divng-app><p>{{5+5}}</p></div>
<divng-app><p>{{10+10}}</p></div>
</body>


以下为ng-app添加属性值的写法都是错误的,会报错

<bodyng-app=“myApp”>
<div><p>{{5+5}}</p></div>
<div><p>{{10+10}}</p></div>
</body>


<body>
<divng-app=“app1”><p>{{5+5}}</p></div>
<divng-app=“app2”><p>{{10+10}}</p></div>
</body>



带属性值时候需要手动加载对应ng-app

<body>
<divid="app1"ng-app="app1">{{5+5}}</div>
<divid="app2"ng-app="app2">{{10+10}}</div>
<scripttype="text/javascript">
varapp1=angular.module("app1",[]);
varapp2=angular.module("app2",[]);
angular.bootstrap(document.getElementById("app2"),['app2']);
</script>
</body>

app1会自动加载

app2需要手动加载


angular.bootstrap() ,手动启动 AngularJS

文档地址 https://docs.angularjs.org/api/ng/function/angular.bootstrap


angular.bootstrap(element, [modules], [config]);

Arguments

Param Type Details
element DOMElement

DOM element which is the root of angular application.

modules

(optional)

Array<String|Function|Array>=

an array of modules to load into the application. Each item in the array should be the name of a predefined module or a (DI annotated) function that will be invoked by the injector as a config block. See: modules

config

(optional)

Object

an object for defining configuration options for the application. The following keys are supported:

  • strictDi - disable automatic function annotation for the application. This is meant to assist in finding bugs which break minified code. Defaults to false.


element应该对应根ng-app的id,

modules 模块名数组

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读