AngularJS – 跟著Google玩網頁!
今天馬老師要與大家分享的是一個前端開發的函式庫AngularJS,前面的文章中都有提到,因為近年來行動裝置的普及讓Javascript大為活躍,不過傳統的Javascript在相容性上、操作上都非常的不方便,所以才有了許許多多Javascript的函式庫與框架的誕生,例如:jQuery、KnockoutJS、GSAP、PaperJS…等,每個Javascript的函式庫和框架的大小、作用和目的都不盡相同,有的是為了特效;有的則是為了方便操作,那今天為何要跟大家分享AngularJS呢?因為…它是名牌!
為何說AngularJS是名牌呢?因為目前他是由Google來負責維護。初始版本是在2009年誕生的,比jQuery晚了三年,言下之意就會比jQuery好嗎?當然也不能這麼說,jQuery在目前還是一個非常火燙的前端開發函式庫,應該要說他們兩個在使用的架構和目的上會有所不同,這篇文章也來比較一下同樣的案例,在兩種語法上面撰寫的不同。
不過首先我們要知道基本的AngularJS開發方式,今天的案例我們利用JS Bin的線上開發網頁來說明,大家也可以到這個平台上來試著玩看看!如果需要利用AngularJS開發自己的網頁,也可以到AngularJS的官方網頁把JS下載到資料夾之後引用到網頁裡。
1. 首先點選Add library後選擇AngularJS Stable,這樣該平台就會加入AngularJS的函式庫進入頁面。
2. 在標籤中,加入ng-app的屬性,讓整份文件都可以使用AngularJS。
3. 接下來就可以在左手邊body標籤內開始輸入一些程式碼來測試,AngularJS在輸出方面有個基本的結構式「{{ }}」,大家輸入以下的程式碼來測試看看結果:
<!DOCTYPE html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <p>{{ '馬老師雲端研究室' }}</p> <p>35+62={{ 35+62 }}</p> <p>NT{{ 62000 | currency }}</p> </body> </html>
可以看到輸出的結果應該是:(在JS Bin上面看AngularJS輸出結果)
馬老師雲端研究室 35+62=97 NT$62,000.00
同樣的如果我們要利用jQuery來完成這樣的頁面,程式碼就會變成:(在JS Bin上面看jQuery輸出結果)
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <p></p> <p></p> <p></p> </body> </html> <script> $("p:eq(0)").text('馬老師雲端研究室') $("p:eq(1)").text('35+62=' + (35 + 62)) $("p:eq(2)").text('NT' + (numeral(62000).format('$ 0,0.00'))) </script>
請注意,為了要完成第三項貨幣型式的呈現,我們還必須去引用這個網站的jQuery Plugin才有辦法比較輕鬆的實現。從上面的案例可以看的出來jQuery是由HTML DOM來抓到對象,再針對對象做操作,而AngularJS不管顯示的效果為何,就是在需要有程式運算的地方出現,所以AngularJS也被稱為符合MVC結構的Javascript函式庫(MVC是一種寫程式的結構,全名是Model View Controller,大家可以上網Google就可以查到很多與MVC相關的資料),但如果你有進入官網,可以看到官網上他的標題是「AngularJS – Superheroic JavaScript MVW Framework」,翻譯成中文大概是「AngularJS是一個符合MV…Whatever什麼什麼隨便啦!的超級函式庫」,可以看出Google的惡搞功夫。
再來示範第二個案例,本案例利用購物行為來製作相關的頁面讓大家參考,程式碼如下:
<!DOCTYPE html> <html ng-app> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body ng-init="stock=120;price=325;quantity=1;total=0"> <p>馬老師雲端研究室 滑鼠墊</p> <p>剩餘:<span ng-model="stock">{{ stock-quantity }}</span>個</p> <p>單價:<span ng-model="price">{{ price | currency:"NT$":0 }}</span>元</p> <p>數量:<input type="number" min="0" max="{{ stock }}" ng-model="quantity"></p> <p>總金額:<span ng-model="total">{{ price*quantity | currency:"NT$":0 }}</span></p> </body> </html>
輸出結果:(在JS Bin上面看AngularJS輸出結果)
在這個案例裡面有四個項目,分別是商品剩餘數量、單價、數量、總金額,使用者唯一可以改變的只有數量,但頁面中會自動運算剩餘數量和總金額,其中在語法body標籤中,加上了ng-init屬性,代表設定這幾個項目的預設值,stock(剩餘數量)120個、price(單價)325元、quantity(數量)1個,total(總金額)325元。接下來在span內的ng-model就是去綁定該定義項目,{{ }}則是顯示該項目或運算結果,另外貨幣格式的部分,也是用了一些設定讓貨幣在這個案例裡面顯示得更為接近台幣格式,另外還是用HTML5 input標籤的min和max來限制欄位內的值。 而這個案例如果轉換為jQuery的寫法,大概會是下面這樣子:(在JS Bin上面看jQuery輸出結果)
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <p>馬老師雲端研究室 滑鼠墊</p> <p>剩餘:<span id="stock"></span>個</p> <p>單價:<span id="price"></span>元</p> <p>數量:<input type="number" min="0" ng-model="quantity" id="quantity"></p> <p>總金額:<span id="total"></span>元</p> </body> </html> <script> var stock = 120; var price = 325; var quantity = 1; var total = 325; $("#stock").text(stock) $("#price").text('NT' + (numeral(price).format('$ 0,0'))) $("#quantity").val(quantity).attr( "max", stock) $("#total").text('NT' + (numeral(total).format('$ 0,0'))) $("#quantity").change(function () { quantity = $(this).val() newstock = stock - $(this).val() total = price * $(this).val() $("#stock").text( newstock) $("#price").text('NT' + (numeral(price).format('$ 0,0'))) $("#total").text('NT' + ( numeral(total).format('$ 0,0'))) }) </script>
後記:以上兩個案例示範的是jQuery和AngularJS在撰寫頁面的不同,不過因為這篇文章原本就是要以AngularJS為主,所以挑的案例當然會是AngularJS比較佔優勢的內容,還是跟我常說的一樣,做不同的東西就要用不同的工具,用錯了工具不是做不到,但可能是會事倍功半的!另外文章中的程式碼,都是嵌入遠端的JS檔,所以也可以把整個程式碼複製到你熟悉的網頁開發軟體內,一樣會看到同樣的結果喔!
https://medium.com/@1kelly76/%EB%B0%9C%EA%B8%B0%EB%B6%80%EC%A0%84-%EA%B7%B9%EB%B3%B5%EC%9D%84-%EC%9C%84%ED%95%9C-%ED%95%84%EC%88%98-%EC%83%9D%ED%99%9C-%EC%8A%B5%EA%B4%80-7ecd345e0682
https://medium.com/@nsw5288/%EB%B9%84%EC%95%84%EA%B7%B8%EB%9D%BC-%EA%B5%AC%EB%A7%A4-%EC%A0%84-%EA%BC%AD-%EC%95%8C%EC%95%84%EC%95%BC-%ED%95%A0-%EA%B1%B4%EA%B0%95-%EC%A0%95%EB%B3%B4-fa4b72b8874f
Have you ever considered about adding a little bit more
than just your articles? I mean, what you say is valuable and everything.
But imagine if you added some great photos or video clips to give your posts more, “pop”!
Your content is excellent but with pics and video clips, this website could definitely be one of the most beneficial in its field.
Great blog!
Review my website lottery defeated software scam
An interesting discussion is worth comment. There’s no doubt that that you need to publish more about this issue, it may not be a taboo subject but usually folks don’t speak about these subjects. To the next! Many thanks!
https://medium.com/@nsw5288/%EB%B9%84%EC%95%84%EA%B7%B8%EB%9D%BC%EB%A5%BC-%EC%A0%80%EB%A0%B4%ED%95%98%EA%B2%8C-%EA%B5%AC%EB%A7%A4%ED%95%98%EB%8A%94-%EB%B2%95-%EA%B0%80%EA%B2%A9-%EC%A0%88%EA%B0%90-%ED%8C%81-0d6cba0d9d92
https://witty-apple-dd3cm1.mystrikingly.com/blog/10
https://inky-owl-dd3cmv.mystrikingly.com/blog/3cf53348e92
https://medium.com/@carlfrancoh38793/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%95%84%EC%9D%B4%EB%94%94-%EC%A0%95%EB%A7%90-%ED%95%84%EC%9A%94%ED%95%9C%EA%B0%80-%EA%B5%AC%EB%A7%A4-%EC%9D%B4%EC%9C%A0-10%EA%B0%80%EC%A7%80-72aec4ea7fe1
https://naveridbuy.exblog.jp/37090926/
https://naveridbuy.blogspot.com/2024/12/blog-post_2.html
https://gold-gull-dd3cmf.mystrikingly.com/blog/fa06ca9b8e6
of course like your web-site but you have to check the spelling on quite a few of your posts.
Several of them are rife with spelling problems and I find it very bothersome to tell the truth nevertheless I’ll surely come back again.
Also visit my web page – lottery defeated software price
https://inky-owl-dd3cmv.mystrikingly.com/blog/f111c3c7d0d
I couldn’t refrain from commenting. Perfectly written!
https://medium.com/@nsw5288/%EB%B9%84%EC%95%84%EA%B7%B8%EB%9D%BC-%EA%B5%AC%EB%A7%A4-%EC%8B%9C-%EC%A0%95%ED%92%88%EC%9D%84-%EC%B0%BE%EB%8A%94-%EA%BF%80%ED%8C%81-3b70adbbe6e9
https://ko.anotepad.com/note/read/jiy4hfbe
https://gold-gull-dd3cmf.mystrikingly.com/blog/dc52558c085
https://witty-apple-dd3cm1.mystrikingly.com/blog/6e9742ef068
https://naveridbuy.blogspot.com/2024/11/2024.html
https://adaptable-camellia-dd3cm4.mystrikingly.com/blog/70aed56e7e8
What’s up to every one, the contents existing at this website are genuinely remarkable for people experience, well, keep up the nice work fellows.
https://arsenal43.ru/image/pages/?promokod_fonbet_bonus_1.html
https://vermilion-elephant-dd3cm3.mystrikingly.com/blog/269c2a5a208
https://medium.com/@nsw5288/%EB%B9%84%EC%95%84%EA%B7%B8%EB%9D%BC-%EA%B5%AC%EB%A7%A4-%EC%8B%9C-%EC%B2%B4%ED%81%AC%EB%A6%AC%EC%8A%A4%ED%8A%B8-%EC%8B%A4%EC%88%98-%EC%97%86%EC%9D%B4-%EC%95%88%EC%A0%84%ED%95%98%EA%B2%8C-%EA%B5%AC%EC%9E%85%ED%95%98%EA%B8%B0-b5e5644c7578
promethazine 25mg over the counter – ciplox order online lincocin 500mg ca
I truly love your site.. Very nice colors & theme. Did you create this amazing site yourself? Please reply back as I’m planning to create my very own blog and would love to find out where you got this from or what the theme is named. Thank you!
https://hallbook.com.br/blogs/376903/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%95%84%EC%9D%B4%EB%94%94-%EA%B1%B0%EB%9E%98%EB%8A%94-%EC%95%88%EC%A0%84%ED%95%9C%EA%B0%80-%EC%A0%84%EB%AC%B8%EA%B0%80%EC%9D%98-%EC%A1%B0%EC%96%B8
https://medium.com/@carlfrancoh38793/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%95%84%EC%9D%B4%EB%94%94-%EA%B5%AC%EB%A7%A4-%EC%88%A8%EA%B2%A8%EC%A7%84-%EC%A7%84%EC%8B%A4%EC%9D%84-%ED%8C%8C%ED%97%A4%EC%B3%90%EB%B4%A4%EC%8A%B5%EB%8B%88%EB%8B%A4-ad07e5d2c8d7
https://inky-owl-dd3cmv.mystrikingly.com/blog/570378fab9f
https://hallbook.com.br/blogs/376901/%EB%84%A4%EC%9D%B4%EB%B2%84-%EC%95%84%EC%9D%B4%EB%94%94-%EA%B1%B0%EB%9E%98%EB%8A%94-%EC%95%88%EC%A0%84%ED%95%9C%EA%B0%80-%EC%A0%84%EB%AC%B8%EA%B0%80%EC%9D%98-%EC%A1%B0%EC%96%B8
https://naveridbuy.exblog.jp/37152138/