Angular的Bootstrap(引導(dǎo))和Compiler(編譯)機制
在上節(jié)簡單介紹了Angular js框架,在這節(jié)將繼續(xù)Angular的Bootstrap(引導(dǎo))和Compiler(編譯)機制。
一:Bootstrap:Angular的初始化
1:Angular推薦的自動化初始如下:
<!doctype html> <html xmlns:ng="http://angularjs.org" ng-app> <body> ... <script src="angular.js"> </body> </html
利用ngapp標示你需要自動引導(dǎo)應(yīng)用程序的根節(jié)點,一般典型為html tag。在DOMContentLoaded事件觸發(fā)Angular會自動尋找ngapp作為應(yīng)用的根節(jié)點,如果找到則會進行如下操作:
1.加載module(模塊)相關(guān)directive(指令)。
2.創(chuàng)建應(yīng)用程序injector(Angular的注入機制).
3.編譯處理ng-app作為根節(jié)點的指令。這里允許你自定義選擇DOM節(jié)點作為應(yīng)用根節(jié)點。
<!doctype html> <html ng-app="optionalModuleName"> <body> I can add: {{ + }}. <script src="angular.js"></script> </body> </html>
2:手動初始化:
如果想對對初始化有更多的控制權(quán),可以采用自定義手動引導(dǎo)方法初始化代替angular的自動初始化。比如你需要在angular編譯模板之前做一些事情,比如改變模板某些內(nèi)容。手動引導(dǎo)方式將會如下:
<!doctype html> <html xmlns:ng="http://angularjs.org"> <body> Hello {{'World'}}! <script src="http://code.angularjs.org/angular.js"></script> <script> angular.element(document).ready(function() { angular.bootstrap(document); }); </script> </body> </html>
1.在頁面所有代碼加載完成后,找到html模板根節(jié)點(典型為document元素).
2.調(diào)用api/angular.bootstrap(angular.bootstrap(element[, modules]))編譯模板使其可執(zhí)行.
二:Compiler:Angular的編譯
Angular的編譯機制允許開發(fā)人員給瀏覽器添加新的Html語法,允許我們添加一些html節(jié)點,attribute,甚至創(chuàng)建一些自定義的節(jié)點,attribute。Angular把這些行為的擴展成為指令directives.Angular帶來了有用的directive,并允許我們創(chuàng)建特定領(lǐng)域的directive。
1: Compiler處理分為兩個步驟:
1.轉(zhuǎn)換DOM,收集directive,返回Link(連接)function。
2.合并指令和Scope產(chǎn)生一個活生生的View。scop mode中的任何改變都會通過反應(yīng)到view中,并來自view的用戶交互也會同步到scope model,并scope是一個單一數(shù)據(jù)源。
2:指令Directive
Directive是一個會被特殊的html設(shè)計編輯處理的行為。其可以被放置在節(jié)點的names, attributes, class 上,甚至是html注釋中。下面是Angular自帶的ng-bind的等價寫法:
<span ng-bind="exp"></span> <span class="ng-bind: exp;"></span> <ng-bind></ng-bind> <!-- directive: ng-bind exp –>
directive僅僅是一個在dom中會被Angular執(zhí)行的一個function。下面是一個拖拽的實例,其可以被應(yīng)用于span,div的attribute上:
angular.module('drag', []).directive('draggable', function ($document) { var startX = , startY = , x = , y = ; return function (scope, element, attr) { element.css({ position: 'relative', border: 'px solid red', backgroundColor: 'lightgrey', cursor: 'pointer' }); element.bind('mousedown', function (event) { startX = event.screenX - x; startY = event.screenY - y; $document.bind('mousemove', mousemove); $document.bind('mouseup', mouseup); }); function mousemove(event) { y = event.screenY - startY; x = event.screenX - startX; element.css({ top: y + 'px', left: x + 'px' }); } function mouseup() { $document.unbind('mousemove', mousemove); $document.unbind('mouseup', mouseup); } } });
Demo
you can drag and move me to anywhere !
3:view理解
有許多的模板引擎都被設(shè)計為模板(template)和數(shù)據(jù)(model)的合并返回一個字符串,再利用innerHTML追加在DOM節(jié)點,這以為則數(shù)據(jù)的任何改變都必須重新合并生成新的內(nèi)容追加在DOM上。形如下圖屬于單向綁定技術(shù):
而Angular則不同利用directive指令而非字符串,返回值是一個合并數(shù)據(jù)model的link function。view和model的綁定是自動,透明的,不需要開發(fā)人員添加額外的action去更新view,Angular在這里不僅是數(shù)據(jù)model的綁定,還有行為概念。作為雙向的綁定,形如下圖:
資料:
1.Angular官網(wǎng):http://angularjs.org/
2.代碼下載:https://github.com/angular/angular.js
以上所述是小編給大家介紹的Angular的Bootstrap(引導(dǎo))和Compiler(編譯)機制,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
AngularJS基礎(chǔ) ng-include 指令示例講解
本文主要介紹AngularJS ng-include 指令,這里對ng-include 指令的知識做了詳細整理介紹,有需要的朋友可以參考下2016-08-08Angular2學(xué)習筆記——詳解NgModule模塊
這篇文章主要介紹了Angular2學(xué)習筆記——詳解NgModule模塊,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12詳解angular2采用自定義指令(Directive)方式加載jquery插件
本篇文章主要介紹了詳解angular2采用自定義指令(Directive)方式加載jquery插件 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02