Node.js 使用AngularJS的方法示例
做一個Web應用,一般都有前臺和后臺,Node.js可以實現(xiàn)后臺,利用jade模板引擎也可以生成一些簡單的前臺頁面,但要想開發(fā)出具有實際意義的現(xiàn)代Web應用,還得搭配一個Web前端框架。
AngularJS是一個JavaScript前端框架,對于Node.js來說是一個完美的客戶端庫。AngularJS強制使用MVC(模型-視圖-控制器,Model-View-Controller)框架,而它又使用JavaScript對象作為它的模型,和Node.js特別般配,用AngularJS的某些服務(比如$http)和Node.js通信,交互的對象不需要被轉(zhuǎn)換為其它結(jié)構(gòu)就能同時在前端和后端使用,堪稱完美。
還有一點,AngularJS背靠Google,值得信賴。不過,天朝網(wǎng)絡也真特么絕了,難道僅僅因為這一點,https://angularjs.org/就不能訪問了嗎,就不能訪問了嗎,就不能訪問了嗎!重要的事情說三遍,說三遍,說三遍,你懂的。不過,你可以翻qiang或者買個VPN,訪問就沒問題了。另外,你也可以通過github訪問AngularJS:https://github.com/angular/angular.js。github上還有一個好東東:https://github.com/jmcunningham/AngularJS-Learning,里面列出了各種AngularJS的學習鏈接。最后呢,還有http://www.angularjs.cn/這個中文站,以及很多點綴在互聯(lián)網(wǎng)上的AngularJS資源,Google或百度都可以找到。
AngularJS是什么
AngularJS其實就是一個js庫,一個js文件,幫助我們更好的開發(fā)Web前端。在github上,AngularJS這么介紹自己:
AngularJS lets you write client-side web applications as if you had a smarter browser. It lets you use good old HTML (or HAML, Jade and friends!) as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. It automatically synchronizes data from your UI (view) with your JavaScript objects (model) through 2-way data binding. To help you structure your application better and make it easy to test, AngularJS teaches the browser how to do dependency injection and inversion of control.
Oh yeah and it helps with server-side communication, taming async callbacks with promises and deferreds. It also makes client-side navigation and deeplinking with hashbang urls or HTML5 pushState a piece of cake. Best of all?? It makes development fun!
都是英文的,Are u OK?
按我的理解,這幾點是比較重要的:
- 擴展HTML語法,動態(tài)修改HTML
- 雙向數(shù)據(jù)綁定
- 提供針對前端和后端的各種服務,比如http,http,cookie,window,window,timeout,$document等,方便開發(fā)者
還有很多基于AngularJS的UI庫,幫助我們構(gòu)建復雜的Web UI,比如https://github.com/angular-ui或https://github.com/angular-ui/bootstrap。
AngularJS的學習資源
很多,Google或百度吧。另外推薦:https://github.com/jmcunningham/AngularJS-Learning。
也有很多專門講AngularJS開發(fā)的圖書,不過我沒看過。我看的是《Node.js+MongoDB+AngularJS Web開發(fā)》,我覺得蠻不錯的,涵蓋了MEAN(Node.js-Express-AngularJS-MongoDB)技術(shù)棧,是想用一種語言成就全棧工程師夢想的不錯選擇。
在Node.js中支持AngularJS
AngularJS是一個客戶端的JavaScript庫,要想在Node.js里支持它,只要在HTML模板中嵌入script標記,讓客戶端能獲取到angular.js文件就成了。
比如這樣:
[code]<script src="]
但這基本上是死路一條,因為國內(nèi)Google不通啊。所以,最好是翻qiang或VPN下載下來,部署到你的網(wǎng)站上,然后這樣:
<script src="http://yousite/javascripts/angular-1.4.3.min.js"></script>
在HTML文檔中使用AngularJS
- 這基本上分為四個部分:
- 使用ng-app指令定義應用程序模塊
- 加載在script標簽中定義的angular.js庫
- 在HTML文檔里插入angular相關的指令(directive)
- 實現(xiàn)控制器(一般在一個js文件里)
下面是一個使用AngularJS的HTML文檔:
<!doctype html> <html ng-app="myApp"> <head> <title>Node.js + Express + AngularJS</title> </head> <body> <div ng-controller="myController"> <h3>Favorite Frameworks:</h3> <li ng-repeat="framework in frameworks">{{framework}}</li> </div> <script src="/javascripts/angular-1.4.3.min.js"></script> <script src="/javascripts/frameworks.js"></script> </body> </html>
上面的文檔內(nèi)引用到的frameworks.js內(nèi)容如下:
angular.module('myApp', []). controller('myController', ['$scope', function($scope){ $scope.frameworks = ['Node.js', 'Express', 'AnjularJS']; }]);
把frameworks.html文件放在HelloExpress的public目錄下面,把frameworks.js放在public/javascripts目錄下,運行網(wǎng)站,在瀏覽器打開地址“http://localhost:3000/frameworks.html”,效果如下圖所示:
在jade模板中使用AngularJS
其實jade模板文件里使用AngularJS,只需要將Angular指令嵌入即可,沒什么特別的。如果你有現(xiàn)成的html文檔,也可以使用html轉(zhuǎn)jade的在線工具來轉(zhuǎn)換為jade模板文件,在這里:http://html2jade.org。
前面使用了AnjularJS的HTML文檔,對應的jade模板文件frameworks.jade內(nèi)容如下:
doctype html html(ng-app="myApp") head title Node.js + Express + AngularJS body div(ng-controller="myController") h3 Favorite Frameworks: li(ng-repeat="framework in frameworks") {{framework}} script(src="/javascripts/angular-1.4.3.min.js") script(src="/javascripts/frameworks.js")
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
nodejs結(jié)合socket.io實現(xiàn)websocket通信功能的方法
這篇文章主要介紹了nodejs結(jié)合socket.io實現(xiàn)websocket通信功能的方法,結(jié)合實例形式分析了nodejs結(jié)合socket.io實現(xiàn)websocket通信的具體步驟與相關操作技巧,需要的朋友可以參考下2018-01-01NodeJS父進程與子進程資源共享原理與實現(xiàn)方法
這篇文章主要介紹了NodeJS父進程與子進程資源共享原理與實現(xiàn)方法,結(jié)合實例形式分析了nodejs基于cluster模塊實現(xiàn)父進程與子進程資源共享的相關操作技巧,需要的朋友可以參考下2018-03-03Node.js Continuation Passing Style( CPS與
這篇文章主要介紹了Node.js Continuation Passing Style,將回調(diào)函數(shù)作為參數(shù)傳遞,這種書寫方式通常被稱為Continuation Passing Style(CPS),它的本質(zhì)仍然是一個高階函數(shù),CPS最初是各大語言中對排序算法的實現(xiàn)2022-06-06Node.js中使用事件發(fā)射器模式實現(xiàn)事件綁定詳解
這篇文章主要介紹了Node.js中使用事件發(fā)射器模式實現(xiàn)事件綁定詳解,本文一并講解了回調(diào)模式、發(fā)射器模式、事件類型等基礎知識做了補充,需要的朋友可以參考下2014-08-08