温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-19 05:00:58 来源:网络 阅读:819 作者:daweilang 栏目:开发技术


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


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

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


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

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


正确写法

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


只加载第一个块

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


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

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


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



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

<body>
    <div id="app1" ng-app="app1">{{ 5 + 5 }}</div>
    <div id="app2" ng-app="app2">{{ 10 + 10 }}</div>
    <script type="text/javascript">
        var app1 = angular.module("app1", []);
        var app2 = 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

ParamTypeDetails
elementDOMElement      

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 模块名数组


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI