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標籤,下圖案例是希望可以產生三個頁籤。

AngularJS Part5 Slide1
AngularJS Part5 Slide1

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

AngularJS Part5 Slide1
AngularJS Part5 Slide2

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

AngularJS Part5 Slide3
AngularJS Part5 Slide3
AngularJS Part5 Slide4
AngularJS Part5 Slide4

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

AngularJS Part5 Slide5
AngularJS Part5 Slide5

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

AngularJS Part5 Slide6
AngularJS Part5 Slide6

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

AngularJS Part5 Slide7
AngularJS Part5 Slide7

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

AngularJS Part5 Slide8
AngularJS Part5 Slide8

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

AngularJS Part5 Slide9
AngularJS Part5 Slide9

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

AngularJS Part5 Slide10
AngularJS Part5 Slide10

到目前為止,大概就完成了我們希望呈現的頁籤效果,大家可以透過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屬性刪除了。

AngularJS Part5 Slide11
AngularJS Part5 Slide11

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

AngularJS Part5 Slide12
AngularJS Part5 Slide12

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

AngularJS Part5 Slide13
AngularJS Part5 Slide13

這樣一來我們邏輯判斷的部分就會和網頁內容有所區隔,大家也可以透過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;
    };
  });
  
})();

You may also like...

60,775 Responses

  1. KevinBless表示:

    click here to investigate
    Carding

  2. GarrettMom表示:

    sweet bonanza slot sweet bonanza oyna sweet bonanza sweetbonanza1st.com

  3. RobertCex表示:

    yeni Гјyelik bonusu veren siteler: casibom 1st – en gГјvenilir yatД±rД±m siteleri casibom1st.com

  4. GarrettMom表示:

    yasal oyun siteleri slot casino siteleri casino siteleri 2025 casinositeleri1st.shop

  5. RobertCex表示:

    en Г§ok freespin veren slot 2025: casibom giris – slot oyunlarД± isimleri casibom1st.com

  6. GarrettMom表示:

    bahis giriЕџ casibom giris yabancД± Еџans oyunlarД± siteleri casibom1st.shop

  7. RobertCex表示:

    son bahis gГјncel giriЕџ: casino siteleri – casino siteleri casinositeleri1st.com

  8. BradyGem表示:

    en gГјvenilir bahis siteleri hangileri?: casibom guncel adres – yeni siteler bahis casibom1st.com

  9. BradyGem表示:

    guvenilir casino siteleri: slot casino siteleri – canlД± casino bonusu veren siteler casinositeleri1st.com

  10. BradyGem表示:

    para kazandД±ran sohbet siteleri: casibom mobil giris – casino bahis siteleri casibom1st.com

  11. Weldonsib表示:

    para kazandД±ran sohbet siteleri: casibom guncel adres – casД±no casibom1st.com

  12. Gabrielnoind表示:

    Axolotl problems
    As Mexico City grew and became more industrialized, the need for water brought pumps and pipes to the lake, and eventually, “it was like a bad, smelly pond with rotten water,” Zambrano said. “All of our aquatic animals suffer with bad water quality, but amphibians suffer more because they have to breathe with the skin.”
    анальный секс смотреть
    To add to the axolotls’ problems, invasive fish species such as carp and tilapia were introduced to the lake, where they feed on axolotl eggs. And a 1985 earthquake in Mexico City displaced thousands of people, who found new homes in the area around the lake, further contributing to the destruction of the axolotls’ habitat.

    These combined threats have devastated axolotl populations. According to the International Union for Conservation of Nature, there are fewer than 100 adult axolotls left in the wild. The species is considered critically endangered.
    While the wild axolotls of Lake Xochimilco have dwindled to near-extinction, countless axolotls have been bred for scientific laboratories and the pet trade. “The axolotl essentially helped establish the field of experimental zoology,” Voss said.

    In 1864, a French army officer brought live axolotls back to Europe, where scientists were surprised to learn that the seemingly juvenile aquatic salamanders were capable of reproduction. Since then, scientists around the world have studied axolotls and their DNA to learn about the salamanders’ unusual metamorphosis (or lack thereof) as well as their ability to regrow injured body parts.
    In addition to their role in labs, axolotls have become popular in the exotic pet trade (though they are illegal to own in California, Maine, New Jersey and Washington, DC). However, the axolotls you might find at a pet shop are different from their wild relatives in Lake Xochimilco. Most wild axolotls are a dark grayish brown. The famous pink axolotls, as well as other color variants such as white, blue, yellow and black, are genetic anomalies that are rare in the wild but selectively bred for in the pet trade.

    What’s more, “most of the animals in the pet trade have a very small genetic variance,” Zambrano said. Pet axolotls tend to be inbred and lack the wide flow of different genes that makes up a healthy population in the wild. That means that the axolotl extinction crisis can’t simply be solved by dumping pet axolotls into Lake Xochimilco. (Plus, the pet axolotls likely wouldn’t fare well with the poor habitat conditions in the lake.)

    Fame and misfortune
    The difficulties that axolotls face in the wild are almost diametrically opposed to the fame they’ve found in recent years. Axolotls have captured the human imagination for centuries, as evidenced by their roles in Aztec religion and stories, but the early 21st century seems to be a high point for them. An axolotl graces the 50 peso bill. There are axolotl-inspired Pokemon, and Reddit commenters have noted that the character Toothless from the “How to Train Your Dragon” movie series is distinctly axolotl-like.

    The introduction of axolotls to Minecraft in 2021 neatly mapped onto an uptick in Google searches for the animals, and social media makes it easy for people to gain access to photos and videos of the salamanders, particularly the photogenic pink ones often kept as pets.

    The axolotl pet trade probably doesn’t directly harm the wild populations since wild salamanders aren’t being poached or taken from Lake Xochimilco. However, Zambrano said, axolotls’ ubiquity in pop culture and pet stores might make people assume that because axolotls “live in all the tanks around the world, they are not in danger.”

  13. Allanbup表示:

    Why axolotls seem to be everywhere — except in the one lake they call home
    анальный секс зрелых

    Scientist Dr. Randal Voss gets the occasional reminder that he’s working with a kind of superstar. When he does outreach events with his laboratory, he encounters people who are keen to meet his research subjects: aquatic salamanders called axolotls.

    The amphibians’ fans tell Voss that they know the animals from the internet, or from caricatures or stuffed animals, exclaiming, “‘They’re so adorable, we love them,’” said Voss, a professor of neuroscience at the University of Kentucky College of Medicine. “People are drawn to them.”

    Take one look at an axolotl, and it’s easy to see why it’s so popular. With their wide eyes, upturned mouths and pastel pink coloring, axolotls look cheerful and vaguely Muppet-like.

    They’ve skyrocketed in pop culture fame, in part thanks to the addition of axolotls to the video game Minecraft in 2021. These unusual salamanders are now found everywhere from Girl Scout patches to hot water bottles. But there’s more to axolotls than meets the eye: Their story is one of scientific discovery, exploitation of the natural world, and the work to rebuild humans’ connection with nature.

    A scientific mystery
    Axolotl is a word from Nahuatl, the Indigenous Mexican language spoken by the Aztecs and an estimated 1.5 million people today. The animals are named for the Aztec god Xolotl, who was said to transform into a salamander. The original Nahuatl pronunciation is “AH-show-LOAT”; in English, “ACK-suh-LAHT-uhl” is commonly used.
    Axolotls are members of a class of animals called amphibians, which also includes frogs. Amphibians lay their jelly-like eggs in water, and the eggs hatch into water-dwelling larval states. (In frogs, these larvae are called tadpoles.)

    Most amphibians, once they reach adulthood, are able to move to land. Since they breathe, in part, by absorbing oxygen through their moist skin, they tend to stay near water.

    Axolotls, however, never complete the metamorphosis to a land-dwelling adult form and spend their whole lives in the water.

    “They maintain their juvenile look throughout the course of their life,” Voss said. “They’re teenagers, at least in appearance, until they die.”

  14. Davidled表示:

    https://sweetbonanza1st.com/# sweet bonanza 1st

  15. Davidled表示:

    http://sweetbonanza1st.com/# sweet bonanza oyna

  16. cbridge bridge assets

  17. cbridge binance

  18. cbridge bridge crypto assets

  19. Weldonsib表示:

    casino siteleri: deneme bonusu veren siteler – slot casino siteleri casinositeleri1st.com

  20. RobertCex表示:

    en saДџlam bahis siteleri: guvenilir casino siteleri – deneme bonusu veren siteler casinositeleri1st.com

  21. RobertCex表示:

    sweet bonanza 1st: sweet bonanza 1st – sweet bonanza sweetbonanza1st.shop

  22. Weldonsib表示:

    sweet bonanza yorumlar: sweet bonanza oyna – sweet bonanza sweetbonanza1st.shop

  23. Davidled表示:

    https://sweetbonanza1st.shop/# sweet bonanza siteleri

  24. RobertCex表示:

    sweet bonanza giris: sweet bonanza demo – sweet bonanza demo sweetbonanza1st.shop

  25. BradyGem表示:

    sweet bonanza yorumlar: sweet bonanza oyna – sweet bonanza yorumlar sweetbonanza1st.shop

  26. Good ? I should definitely pronounce, impressed with your web site. I had no trouble navigating through all the tabs and related info ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Quite unusual. Is likely to appreciate it for those who add forums or something, website theme . a tones way for your customer to communicate. Excellent task..

  27. BradyGem表示:

    lisansl? casino siteleri: casino siteleri 2025 – lisansl? casino siteleri casinositeleri1st.com

  28. BradyGem表示:

    para kazandiran kumar oyunlarД±: guvenilir casino siteleri – guvenilir casino siteleri casinositeleri1st.com

發佈回覆給「Gabrielnoind」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。