Click here to Skip to main content
15,893,508 members
Articles / AngularJs

AngularJS: Controllers: Tutorial 4

Rate me:
Please Sign up or sign in to vote.
2.60/5 (2 votes)
25 Dec 2015CPOL1 min read 8.7K   2  
AngularJS: Controllers: Tutorial 4

Article Series

  1. Tutorial 1: Angular JS
  2. Tutorial 2: Controllers
  3. Tutorial 3: Controllers
  4. Tutorial 4: Controllers: Advanced
  5. Tutorial 5: Models
  6. Tutorial 6: Services

Prerequisites: AngularJS: Controllers: Tutorial 3

In this tutorial, we will look at more of the beautification aspect using few more directives.We will use the same Book Store Application and try to create tabs for Description and Reviews.

We would modify our earlier HTML code as below:

HTML
<!DOCTYPE html>
<html lang="en" ng-app="Namespace">
 
<head>
<meta charset="utf-8">
<title>Hello World</title>
    <link rel="stylesheet" type="text/css" href="css/templates.css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js">
 
 </script>
<script src="javascript/app1.js"></script>
</head>
 
<body ng-controller="ControllerName as control">
Hello {{name}}
<h1>Welcome to my book store</h1>
<div ng-repeat="arr in bookarray">
<table>
<tr>
<td>
<div>
<img ng-src="{{arr.bookImage}}" />
</div></td>
<td>
<div>
<div>
<b>Book Name :</b>{{arr.bookName}}</div>
<div>
<b>Book Cost :</b>{{arr.bookCost | currency}}</div>
</div></td>
</tr>
</table>
<section ng-controller="PanelSwitcher as panel">
<ul class="book tabs" id="menu">
    <li ng-class="{ active:panel.isSelected(1) }"> 
	<a href ng-click="panel.selectTab(1)">Description</a></li>
    <li ng-class="{ active:panel.isSelected(2) }"> 
	<a href ng-click="panel.selectTab(2)">Reviews</a></li>
</ul>
<div id="panel1" ng-show="panel.isSelected(1)">
{{arr.bookDescription}}
</div>
<div id="panel2" ng-show="panel.isSelected(2)">
<form name="reviewForm" ng-controller="ReviewController as reviewCtrl">
<blockquote ng-repeat="review in arr.reviews">
<b>Stars: {{review.stars}}</b> {{review.body}}
<cite>by: {{review.author}}</cite></blockquote>
<select name="dropdown" ng-model="review.stars">
<option value="1">1 star</option>
<option value="2">2 stars</option>
<option value="2">3 stars</option>
<option value="2">4 stars</option>
<option value="2">5 stars</option>
</select>
<textarea placeholder="Comments" ng-model="review.body"></textarea>
<label>by:</label>
<input type="email" placeholder="xyz@abc.com" ng-model="review.author" />
<input type="submit" value="Submit" />
</form>
</div>
</section>
</div>
</body>
 
</html>

From the above, we have added a href for tabs and then made use of ‘ng-click’. We have also used ‘ng-show’ for displaying a particular tab at once. This is equivalent to conditional visibility. ‘active:tab’ is used to show to clicked or focused element.

But from the above, we have a lot of code in the HTML. This is like our View and we would like our view to be clean and with very little code. So we will again make use of the Controller and move the selection of tab and displaying of tab in a new controller as below:

JavaScript
Namespace.controller('PanelSwitcher', function () {
this.tab = 1;
this.selectTab = function(setTab){
 this.tab = setTab;
};
 
this.isSelected = function(checkTab){
 return this.tab === checkTab;
};
 
})

We have just created 2 methods/functions which are set the tab and receive the tab.
And then, we have modified our HTML code as below which looks much more cleaner now !!

HTML
<body ng-controller="ControllerName as control">
 
Hello {{name}}
<h1>Welcome to my book store</h1>
<div ng-repeat="arr in bookarray">
<table>
<tr>
<td>
<div>
<img ng-src="{{arr.bookImage}}" />
</div></td>
<td>
<div>
<div>
<b>Book Name :</b>{{arr.bookName}}</div>
<div>
<b>Book Cost :</b>{{arr.bookCost | currency}}</div>
</div></td>
</tr>
</table>
<section ng-controller="PanelSwitcher as panel">
<ul class="book tabs" id="menu">
    <li ng-class="{ active:panel.isSelected(1) }"> 
	<a href ng-click="panel.selectTab(1)">Description</a></li>
    <li ng-class="{ active:panel.isSelected(2) }"> 
	<a href ng-click="panel.selectTab(2)">Reviews</a></li>
</ul>
<div id="panel1" ng-show="panel.isSelected(1)">
 
{{arr.bookDescription}}
 
</div>
<div id="panel2" ng-show="panel.isSelected(2)">
 
No Reviews
 
</div>
</section>
 
</div>
</body>

The output would look like below:

Tabs with Controllers

We will look at how we can ask users to input data using Forms and Models in the next post. Happy coding !!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Passionate about Microsoft Technologies like WPF, Windows Azure, ASP.NET, Win Phone and also on Cross platform Mobile Apps, Mongo DB's, IOT & WOT. I love learning and working with (or) Creating Design Patterns . Writer | Technology Evangelist | Technology Lover | Microsoft Developer Guidance Advisory Council Member | Cisco Champion | Speaker |

Blog : http://adityaswami89.wordpress.com/

Comments and Discussions

 
-- There are no messages in this forum --