Download Source Code
Download the source code from here: Dynamic Angular JS Tabs In MVC
Background
Recently I have got a requirement to create a tab control in one of my Angular JS application. Then I thought of creating the same dynamically according to the user needs. This article is a demo of the same.
Using the code
First, we will start with creating an MVC application. Open your visual studio, then click File->New->Project. Name your project.
Now our application is launched, please go ahead and install Angular JS in your project from NuGet packages. You can see some new CSS files and scripts has been added to our project. So the set up has been done. Now what we need to do is to move on the coding part.
Create a controller
Now we can create a new controller, in my case I created a controller ‘HomeController’. In my controller I am going to call a model action which will return some dynamic data. See the code below.
public
class
HomeController : Controller
{
//
// GET: /Home/
ActionResult Index()
return
View();
}
JsonResult TabData()
Test t =
new
Test();
var myList = t.GetData();
Json(myList, JsonRequestBehavior.AllowGet);
Here we have one ActionResult and one JsonResult which we are going to call as Angular JS service. As you can see I am creating an instance of my model Test, now we will create our model class. Shall we?
Create Model
I have create a model class with the name Test. Here I am creating some data dynamically using a for loop and assign those values to a list. Please see the codes below.
namespace
AsyncActions.Models
Test
List<Customer> GetData()
try
List<Customer> cst =
List<Customer>();
for
(
int
i = 0; i < 15; i++)
Customer c =
Customer();
c.CustomerID = i;
c.CustomerCode =
"CST"
+ i;
cst.Add(c);
cst;
catch
(Exception)
throw
NotImplementedException();
Customer
CustomerID {
get
;
set
; }
string
CustomerCode {
As you can see I am creating a list of type Customer. Is that fine? Now what is pending? Yes, a view.
Create a view
Right click on your controller and click add view, that will give you a new view in your view folder. Hope you get that.
So our view is ready, now we can do some codes in our view to populate our tab. Are you ready?
<
div
ng-controller
=
"tabController"
"sample tabsdemoDynamicTabs"
layout
"column"
ng-cloak
""
ng-app
"tab"
>
md-content
"md-padding"
md-tabs
md-selected
"selectedIndex"
md-border-bottom
md-autoselect
md-tab
ng-repeat
"tab in tabs"
ng-disabled
"tab.disabled"
label
"{{tab.title}}"
"demo-tab tab{{$index}}"
style
"padding: 25px; text-align: center;"
ng-bind
"tab.content"
></
br
md-button
"md-primary md-raised"
ng-click
"removeTab( tab )"
"tabs.length <= 1"
>Remove Tab</
</
form
ng-submit
"addTab(tTitle,tContent)"
"padding-top: 0;"
"row"
layout-sm
flex
"position: relative;"
h2
"md-subhead"
"position: absolute; bottom: 0; left: 0; margin: 0; font-weight: 500; text-transform: uppercase; line-height: 35px; white-space: nowrap;"
>Add a new Tab:</
md-input-container
"label"
>Label</
input
type
"text"
id
ng-model
"tTitle"
"content"
>Content</
"tContent"
"add-tab md-primary md-raised"
"!tTitle || !tContent"
"submit"
"margin-right: 0;"
>Add Tab</
As you can see we are declaring our angular js controller and app name as follows.
ng-controller="tabController" class="sample tabsdemoDynamicTabs" layout="column" ng-cloak="" ng-app="tab"
Now we will add the needed reference to our view.
Add the style sheet references
link
href
"~/CSS/angular-material.css"
rel
"stylesheet"
/>
"~/CSS/docs.css"
Add styles for tabs
.tabsdemoDynamicTabs md-content {
background-color: transparent !important;
.tabsdemoDynamicTabs md-content md-tabs {
border: 1px solid #e1e1e1;
.tabsdemoDynamicTabs md-content md-tabs md-tab-content {
background: #f6f6f6;
.tabsdemoDynamicTabs md-content md-tabs md-tabs-wrapper {
background: white;
.tabsdemoDynamicTabs md-content h1:first-child {
margin-top: 0;
.tabsdemoDynamicTabs md-input-container {
padding-bottom: 0;
.tabsdemoDynamicTabs .remove-tab {
margin-bottom: 40px;
.tabsdemoDynamicTabs .demo-tab > div > div {
padding: 25px;
box-sizing: border-box;
.tabsdemoDynamicTabs .edit-form input {
width: 100%;
.tabsdemoDynamicTabs md-tabs {
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
.tabsdemoDynamicTabs md-tab[disabled] {
opacity: 0.5;
.tabsdemoDynamicTabs label {
text-align: left;
.tabsdemoDynamicTabs .long > input {
width: 264px;
.tabsdemoDynamicTabs .md-button.add-tab {
transform: translateY(5px);
Add the JS references
script
src
"~/Scripts/angular.min.js"
"~/Scripts/angular-animate.min.js"
"~/Scripts/angular-route.js"
"~/Scripts/angular-aria.min.js"
"~/Scripts/angular-messages.min.js"
"~/Scripts/svg-assets-cache.js"
"~/Scripts/angular-material.js"
"~/Scripts/Module.js"
Here Module.js is the file where we are creating our Angular JS controller, Service, App. So can we go ahead and create those?
Create an app, controller, service in Angular JS
To add an app, controller, service in Angular JS, you need to add the codes as below.
function
() {
'use strict'
var
app;
//create app
app = angular.module(
, [
'ngMaterial'
,
'ngMessages'
'material.svgAssetsCache'
]);
//create controller
app.controller(
'tabController'
($scope, tabService) {
serv = tabService.getAll();
serv.then(
(d) {
tabController(d.data, $scope);
},
(error) {
console.log(
'Something went wrong, please check after a while'
);
})
});
//create service
app.service(
'tabService'
($http) {
this
.getAll =
$http({
url:
"/Home/TabData"
//Here we are calling our controller JSON action
method:
"GET"
};
})();
As you can see once we get the data from the Angular JS service (tabService) to the controller (tabController), we are passing the data to a function named tabController. Below is the code for that function. function tabController(data, $scope) {
tabController(data, $scope) {
tabsArray = [];
i = 0; i < data.length; i++) {
tabsArray.push(
'title'
:
"Customer ID: "
+ data[i].CustomerID,
'content'
: data[i].CustomerCode +
" The data you are seeing here is for the customer with the CustomerCode "
+ data[i].CustomerCode
tabs = tabsArray,
selected =
null
previous =
$scope.tabs = tabs;
$scope.selectedIndex = 0;
$scope.$watch(
'selectedIndex'
(current, old) {
previous = selected;
selected = tabs[current];
$scope.addTab =
(title, view) {
view = view || title +
" Content View"
tabs.push({ title: title, content: view, disabled:
false
$scope.removeTab =
(tab) {
index = tabs.indexOf(tab);
tabs.splice(index, 1);
That’s all we have created the Angular JS tabs dynamically. Shall we see the output now?
Output
Dynamic Angular JS Tabs In MVC Figure 1
Dynamic Angular JS Tabs In MVC Figure 2
References
Angular JS Tabs