AngularJS ng-app的自動載入與屬性值

來源:互聯網
上載者:User

標籤:angularjs


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
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 toassist in finding bugs which break minified code. Defaults to false.


element應該對應根ng-app的id,

modules 模組名數組


AngularJS ng-app的自動載入與屬性值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.