AngularJS – Tab 介紹篇
為了方便文章閱讀,本篇將Tab翻譯成頁籤
這個案例的複雜度會比較前面高一些,因為它不僅僅是使用我們一直提到的AngularJS,為了要製作頁面上面的一些互動效果,還加入了Bootstrape這個通常被用來當RWD的框架,不過也僅僅是套用了幾個類別,所以大家也不用太擔心,接下來我們先看一下這個案例最後希望要達成的效果頁面。
在看過了目標頁面後,我們先來了解一下需要怎麼樣架構我們的HTML,首先是CSS和Javascript的引入,分別是AngularJS、jQuery、Bootstrap CSS以及Javascript:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script src="https://code.jquery.com/jquery.min.js"></script> <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
在HTML文件中,必須有項目清單標籤ul、li,而在ul標籤中需套用nav nav-pills這兩個css類別,這兩個類別是由Bootstrape的css所提供的,li標籤是包覆著超連結的a標籤,下圖案例是希望可以產生三個頁籤。

在a標籤中加入ng-click=”tab = 1″、ng-click=”tab = 2″、ng-click=”tab = 3″去設定當使用者按下連結後tab變數會隨著變化,另外為了方便觀察是否成功,在頁面上利用表達式將tab變數顯示出來。

若一切順利,在我們按下不同的頁籤連結時,畫面上應該會有數字上面的變化。


接下來開始製作點選頁籤後的內容頁面,同樣的內容頁面也應該有三個才對,在HTML中產生三個div,其中套用Bootstrape所提供的CSS panel類別,div的內容部分可依照需求置入。

在div中利用ng-show去判斷tab變數的值來切換顯示。

完成後,在我們點選不同的連結時,內容的部分也應該會隨著變動。

接下來我們在section標籤中設定ng-init=”tab=1″的屬性來決定tab變數的初始值。

接下來在li內新增ng-class的屬性,依tab變數的值來切換active的CSS屬性(該屬性由Bootstrape提供樣式),其中三個連續的等號是判斷該變數與值完全相同的意思。

這個動作的目的是希望當網友點選之後,可以如下圖所示,清楚的標示目前頁面上所顯示的是第幾個項目。

到目前為止,大概就完成了我們希望呈現的頁籤效果,大家可以透過JS Bin來測試看看到目前為止的程式碼。
<!DOCTYPE html> <html ng-app> <head> <meta name="description" content="AngularJS Tabs Example 1"> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script src="//code.jquery.com/jquery.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <meta charset="utf-8"> <title>AngularJS Tabs Example 1</title> </head> <body> <section ng-init="tab=1"> <ul class="nav nav-pills"> <li ng-class="{ active: tab===1 }"> <a href="" ng-click="tab=1">滑鼠墊</a> </li> <li ng-class="{ active: tab===2 }"> <a href="" ng-click="tab=2">馬克杯</a> </li> <li ng-class="{ active: tab===3 }"> <a href="" ng-click="tab=3">杯墊</a> </li> </ul> <div class="panel" ng-show="tab===1"> <h4>馬老師雲端研究室 滑鼠墊</h4> <p>產品介紹...</p> </div> <div class="panel" ng-show="tab===2"> <h4>馬老師雲端研究室 馬克杯</h4> <p>產品介紹...</p> </div> <div class="panel" ng-show="tab===3"> <h4>馬老師雲端研究室 杯墊</h4> <p>產品介紹...</p> </div> </section> </body> </html>
在看完了上面的案例之後,我們可以觀察到程式邏輯判斷的部分都是直接撰寫在HTML頁面上,那如果我們要把邏輯判斷的部分從HTML拆開寫到Javascript檔又應該要如何處理呢?首先,不用說的當然是必須要有應用程式的建立以及控制器囉!下圖中我們開始新增控制器,並且在section標籤中,輸入ng-controller=”panelController as panel”的屬性,相信在看了前幾篇教學的同學們對於這樣的項目是再熟悉不過了!接下來在控制器中,決定tab變數的初始值,就可以把原來的ng-init屬性刪除了。

在ng-click後去執行控制器中的selectTab函數,並且針對該函數帶入不同的值,利用帶入的值來改變tab變數值。

在ng-click後去執行控制器中的isSelected函數,也帶出不同的值給函數,讓函數可以回傳tab===1或2、3這樣的內容給ng-show使用。

這樣一來我們邏輯判斷的部分就會和網頁內容有所區隔,大家也可以透過JS Bin來測試這樣的程式結構。
<!DOCTYPE html> <html ng-app="store"> <head> <meta name="description" content="AngularJS Tabs Example 2"> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script src="//code.jquery.com/jquery.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <meta charset="utf-8"> <title>AngularJS Tabs Example 2</title> </head> <body> <section ng-controller="PanelController as panel"> <ul class="nav nav-pills"> <li ng-class="{ active: panel.isSelected(1) }"> <a href="" ng-click="panel.selectTab(1)">滑鼠墊</a> </li> <li ng-class="{ active: panel.isSelected(2) }"> <a href="" ng-click="panel.selectTab(2)">馬克杯</a> </li> <li ng-class="{ active: panel.isSelected(3) }"> <a href="" ng-click="panel.selectTab(3)">杯墊</a> </li> </ul> <div class="panel" ng-show="panel.isSelected(1)"> <h4>馬老師雲端研究室 滑鼠墊</h4> <p>產品介紹...</p> </div> <div class="panel" ng-show="panel.isSelected(2)"> <h4>馬老師雲端研究室 馬克杯</h4> <p>產品介紹...</p> </div> <div class="panel" ng-show="panel.isSelected(3)"> <h4>馬老師雲端研究室 杯墊</h4> <p>產品介紹...</p> </div> </section> </body> </html>
(function(){ var app = angular.module('store', []); app.controller('PanelController', function(){ this.tab = 1; this.selectTab = function(setTab){ this.tab = setTab; }; this.isSelected = function(checkTab){ return this.tab === checkTab; }; }); })();
онлайн казино рулетка – онлайн казино бесплатно, онлайн казино бонусы
онлайн казино шансы на выигрыш – лучшие онлайн казино, казино онлайн без регистрации
онлайн казино с выводом денег – онлайн казино промокоды, лучшие онлайн казино
Online medicine order: indian pharmacy – india online pharmacy
pharmacies in mexico that ship to usa: Agb Mexico Pharm – mexican online pharmacies prescription drugs
онлайн казино софт – онлайн казино рабочие сайты, как выиграть в онлайн казино
buying prescription drugs in mexico: mexico pharmacies prescription drugs – mexican pharmaceuticals online
reputable canadian pharmacy go canada pharm canada pharmacy
canadian pharmacy online store: go canada pharm – ordering drugs from canada
mexico pharmacies prescription drugs Agb Mexico Pharm Agb Mexico Pharm
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем:сервисные центры в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем:ремонт крупногабаритной техники в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://agbmexicopharm.com/# buying prescription drugs in mexico online
п»їbest mexican online pharmacies: mexican mail order pharmacies – mexican mail order pharmacies
www india pharm www india pharm www india pharm
http://gocanadapharm.com/# canadian online pharmacy reviews
онлайн казино шансы на выигрыш – казино онлайн топ на деньги, лучшие онлайн казино
Polygon Bridge has become an indispensable part of my crypto toolkit. It’s especially useful for those of us who are active in the DeFi space. With its user-friendly interface and reliable performance, I couldn’t ask for more!
https://gocanadapharm.com/# canadian pharmacy online ship to usa
Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!
online pharmacy india: best india pharmacy – www india pharm
www canadianonlinepharmacy: GoCanadaPharm – certified canadian international pharmacy
reputable mexican pharmacies online: buying prescription drugs in mexico – buying from online mexican pharmacy
indianpharmacy com: indian pharmacies safe – www india pharm
https://telegra.ph/12-Kazino-onlajn-2024–Rejting-SpArk-top-100–luchshih-sajtov-v-Rossii-04-05
www india pharm: Online medicine order – mail order pharmacy india
canadian online drugs: go canada pharm – canadapharmacyonline legit
www india pharm: www india pharm – india pharmacy mail order
‘We don’t want the American Dream for our kids’: Why this couple left the US for Ecuador with their children four years ago
онлайн адвокат у військових справах Запоріжжя
They’d never even visited Ecuador before, but Brittany and Blake Bowen, from the United States, decided to move to the South American country in 2021 to give their four children a completely different upbringing.
The Bowens, who were previously based in the state of Washington, have been living in Loja, a small city based in the southern section of the Andes Mountains, ever since, and say that they are in it for the long haul.
“We love this little country,” Brittany tells CNN Travel. “We hope that maybe we’ll have grandkids here one day.”
Before the move, the couple, who’ve been married for nearly 17 years, say that they were becoming increasingly concerned about the pressures placed on children by “modern American society” and wanted to try something new.
“We did not like what we’d seen develop over the course of the last couple decades…” adds Brittany, explaining that they felt that young people in the United States were becoming “more isolated.”
“We weren’t confident that our kids would enjoy the same sort of potential trajectory that previous generations had shared.
“And the more we considered things like that, the more we wondered, ‘Is that even what we want? Do we even want them to be on a fast track to the American Dream?”
The couple were also frustrated with living what they describe as the “standard American life.”
“Long commutes and never enough money,” says Blake. “All those usual problems… I was working in a career that was very time consuming, and took me away from home a lot. So we didn’t want that anymore.”
So why did they choose Ecuador as their “new home”?
1win приложение андроид – 1win партнерская программа, 1win вывод