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; }; }); })();
фонбет беларусь фонбет беларусь .
http://plavixclo.com/# buy clopidogrel online
buy cytotec over the counter: cheapest cytotec – cytotec abortion pill
buy Lisinopril 1st buy lisinopril 2.5 mg online cheapest Lisinopril
гѓ—гѓ¬гѓ‰гѓ‹гѓійЂљиІ©гЃЉгЃ™гЃ™г‚Ѓ – イソトレチノインの購入 イソトレチノインの購入
Hey there! Just felt like dropping by to let you know how much I admire your blog. Your insights on making money online are truly impressive. Making money from home has never been easier thanks to affiliate promotion. It’s all about identifying the ideal goods to promote and establishing relationships with your audience. Your blog is a treasure trove of information for emerging online entrepreneurs. Keep up the great work!
Italy’s working visas are notoriously hard to get. We spoke to Americans who managed it
варочный котел 500 литров
The Italian village offering $1 homes to Americans upset by the US election result was one of our top stories on CNN Travel this week.
Like many other places in rural Italy (such as Sambuca in Sicily), the Sardinian village of Ollolai has an ongoing campaign to persuade outsiders to move there to revive the town’s fortunes. Focusing on the US election result is its latest strategy.
Despite all the houses going on offer, working visas to Italy are still very limited. Professional musicians Zeneba Bowers and Matt Walker gave up their Tennessee home in 2019 and moved to a village north of Rome after securing super-rare self-employed visas. Here’s how they did it.
California woman Chelsea Waite says it was “nothing less than a miracle” when she snagged the new digital nomad visa that launched in April 2024, although there have been few accounts of people getting hold of one. Here’s how it fell into place for the self-employed public relations professional.
For well-heeled Americans who set their sights beyond Italy, the options are greater. Interest in citizenship-by-investment “golden visas” has soared since the election, according to consultants who help the wealthy migrate.
Ham, cheese and bread
Parma ham is one of Italy’s tastiest exports, but fans of the cured delicacy should prepare themselves for potential disappointment as a crisis threatens supplies and drives up prices.
Over in the world of dairy, this year’s World Cheese Awards saw 4,786 cheeses from 47 countries assembled in the Portuguese city of Viseu to face judges’ scrutiny. The winning coagulated curd was a “voluptuous … match of protein and fat,” one juror said.
Finally, in the disputed Kashmir region of India, bakers are producing breads that could rival France. The rich bread culture is a legacy of the Silk Road trade route that once passed through the region.
cheap amoxicillin 500mg: Amoxicillin for sale – amoxicillin 750 mg price
Купить диплом в России, предлагает наша компания
http://plavixclo.com/# cheap plavix antiplatelet drug
purchase amoxicillin online: cheap amoxil – price for amoxicillin 875 mg
Italy’s working visas are notoriously hard to get. We spoke to Americans who managed it
котел для варки кальянного табака
The Italian village offering $1 homes to Americans upset by the US election result was one of our top stories on CNN Travel this week.
Like many other places in rural Italy (such as Sambuca in Sicily), the Sardinian village of Ollolai has an ongoing campaign to persuade outsiders to move there to revive the town’s fortunes. Focusing on the US election result is its latest strategy.
Despite all the houses going on offer, working visas to Italy are still very limited. Professional musicians Zeneba Bowers and Matt Walker gave up their Tennessee home in 2019 and moved to a village north of Rome after securing super-rare self-employed visas. Here’s how they did it.
California woman Chelsea Waite says it was “nothing less than a miracle” when she snagged the new digital nomad visa that launched in April 2024, although there have been few accounts of people getting hold of one. Here’s how it fell into place for the self-employed public relations professional.
For well-heeled Americans who set their sights beyond Italy, the options are greater. Interest in citizenship-by-investment “golden visas” has soared since the election, according to consultants who help the wealthy migrate.
Ham, cheese and bread
Parma ham is one of Italy’s tastiest exports, but fans of the cured delicacy should prepare themselves for potential disappointment as a crisis threatens supplies and drives up prices.
Over in the world of dairy, this year’s World Cheese Awards saw 4,786 cheeses from 47 countries assembled in the Portuguese city of Viseu to face judges’ scrutiny. The winning coagulated curd was a “voluptuous … match of protein and fat,” one juror said.
Finally, in the disputed Kashmir region of India, bakers are producing breads that could rival France. The rich bread culture is a legacy of the Silk Road trade route that once passed through the region.
Купить диплом о среднем образовании в Москве и любом другом городе
cytotec pills buy online: buy cytotec online – buy cytotec online fast delivery
buy Lisinopril online: lisinopril1st – lisinopril1st
Italy’s working visas are notoriously hard to get. We spoke to Americans who managed it
котел для кондитерской
The Italian village offering $1 homes to Americans upset by the US election result was one of our top stories on CNN Travel this week.
Like many other places in rural Italy (such as Sambuca in Sicily), the Sardinian village of Ollolai has an ongoing campaign to persuade outsiders to move there to revive the town’s fortunes. Focusing on the US election result is its latest strategy.
Despite all the houses going on offer, working visas to Italy are still very limited. Professional musicians Zeneba Bowers and Matt Walker gave up their Tennessee home in 2019 and moved to a village north of Rome after securing super-rare self-employed visas. Here’s how they did it.
California woman Chelsea Waite says it was “nothing less than a miracle” when she snagged the new digital nomad visa that launched in April 2024, although there have been few accounts of people getting hold of one. Here’s how it fell into place for the self-employed public relations professional.
For well-heeled Americans who set their sights beyond Italy, the options are greater. Interest in citizenship-by-investment “golden visas” has soared since the election, according to consultants who help the wealthy migrate.
Ham, cheese and bread
Parma ham is one of Italy’s tastiest exports, but fans of the cured delicacy should prepare themselves for potential disappointment as a crisis threatens supplies and drives up prices.
Over in the world of dairy, this year’s World Cheese Awards saw 4,786 cheeses from 47 countries assembled in the Portuguese city of Viseu to face judges’ scrutiny. The winning coagulated curd was a “voluptuous … match of protein and fat,” one juror said.
Finally, in the disputed Kashmir region of India, bakers are producing breads that could rival France. The rich bread culture is a legacy of the Silk Road trade route that once passed through the region.
They’ve sailed across Southeast Asia for centuries. Now, these sea nomads are being forced to live on land
скупка autel
Bilkuin Jimi Salih doesn’t remember how old he was when he learned to dive, only, that all the men in his family can do it.
It might have been his grandfather who taught him, or his father, or even an uncle or cousin. He recalls swimming dozens of feet underwater among the reefs, collecting spider conches, abalone and sea cucumbers to sell at the local fish market.
“One of our specialties is that, because we live on the sea and we’re always in the sea, we can dive in the water for a long time,” says Salih, via a translator. “We learn by observing, and from there, we develop our own technique.”
To most people, Salih’s free diving skills are highly unusual; but not to his community. Salih is Bajau Laut, an indigenous seafaring group in Southeast Asia that has lived a semi-nomadic lifestyle on the ocean for centuries. Living on boats and fishing for income and sustenance, the Bajau Laut aren’t just reliant on the sea: they’re biologically adapted to it, with larger spleens that give them the ability to hold their breath for far longer than the average person.
“We’re very comfortable in the water,” says Salih. The 20-year-old was born on board a lepa, a type of houseboat, on the shore of Omadal Island, off the coast of Semporna in Malaysian Borneo.
Freedom Appliances
Highest quality service in the Calgary !
Calgary appliance repair
Freedom Appliances serve all brands of major appliances in Calgary.
Appliance Repair Services Calgary – are services where a qualified technician will come to you home to diagnose, recommend, and repair a common household appliance. The cost of appliance repair services includes a trip charge, labor, and replacement parts. Since the cost is not insignificant, most people elect to repair their larger appliances including washer, dryer, oven, cooktop, dishwasher, and refrigerator where the cost of replacement is high.
When you call Calgary Appliance Repair, one of our friendly staff members will great you and schedule an appointment, depending on what times work best for you. One of our experienced technicians will arrive at your home and give you a complete diagnosis.
Appliance Repair Services Calgary
Have you been looking for an appliance repair service in Calgary? At Freedom Appliances, we can fix your appliances in a timely manner. Our appliance repair services in Calgary are the number one choice if you need fast and effective repairs. Our technicians have all of the necessary tools to complete almost any appliance repair repair service in Calgary. We will be able to get your appliances working again in no time at all!
Our professional technicians will diagnose the issue with your appliance. And then we’ll come up with the best, most cost effective solution. Because we have everything on hand, we complete a simple repair job or replace a part. Freedom Appliances always provide the costs upfront. We operate with transparency. Our technician won’t begin your Calgary repair services until you give us the green light.
And you won’t find another company that has better customer service than us, we promise! We want our customers to feel satisfied with their appliance repair services in Calgary so much that they would recommend us to their friends and family members. If there are any issues or problems with our work, we will make sure it gets resolved as soon as possible. There is nothing more important than making sure our customers are happy!
When it comes to delivering the best appliance repair services in Calgary, Freedom Appliances are the best. Call Freedom Appliances today for quality appliance repair services on your home appliances – Calgary. At Freedom Appliances, your satisfaction is our priority.
Сколько стоит диплом высшего и среднего образования и как его получить?
generic clomid without insurance: rex pharm – where to buy cheap clomid online
Merci j’ai appris beaucoup avec ton article 🙂 !
Всё, что нужно знать о покупке аттестата о среднем образовании
buy Lisinopril online: buy Lisinopril 1st – cheapest Lisinopril
http://cytpremium.com/# purchase cytotec
amoxicillin no prescription: amoxil com pharm – amoxicillin online pharmacy
Top Earning App in Pakistan|Easy Way to Earn in Pakistan|Innovative Earning Method in Pakistan|Efficient Earning Tool in Pakistan|Rate the Best Earning App in Pakistan|Economic Earning in Pakistan|Processing App in Pakistan: Proven Tool|Best app for earning in Pakistan: time-tested|Interesting opportunities for earning in Pakistan|Pakistan: best app for processing
apps for earning money in pakistan apps to earn money in pakistan .
http://iverfast.com/# ivermectin malaria
Lisinopril 1st cheapest Lisinopril buy Lisinopril 1st
antiplatelet drug: buy plavix online – buy Clopidogrel over the counter
cost cheap clomid pills: clomid purchase online rex pharm – where to get clomid no prescription