Dynamic loading of views as directives in an AngularJS wizard.

Demo and Code can be found at: http://plnkr.co/edit/Ht4ud6kyJUzDTQdS9MI2?p=preview

 

image

 

Just for the fun, I added a D3.js chart:

 

image

 

A simple AngularJS wizard that dynamically loads pages (views).

In the plunker each page is a *.html file that is loaded by dynamically setting a viewmodel property “pageTemplateUrl”:

ng-include="pageTemplateUrl"

Each *.html file contains a “page” directive.

In this examples all pages share one “page” directive, so all logic for all pages is stored in the “page” directive, but you can put in, any custom made directive in the “*.html” file, so you can give each page it’s own directive / logic.

 

Index.html

<body>
<div ng-controller="main">
<h2 class="main-title">{{ title }}</h2>
<wizard></wizard>
</div>
</body>

Wizard.html

<div class="wizard">
<div>Wizard</div>
<div class="page page-slide-in" ng-include="pageTemplateUrl"></div>
<div class="footer flex-horizontal">
<div class="wizard-footer-left">
<button ng-disabled="prevShouldBeDisabled()"
ng-click="prev()">
Back
</button>
</div>
<div class="wizard-footer-middle flex-vertical">
<ul class="steps">
<li ng-repeat="page in pages"
ng-class="{ active: isActive(page), complete: isCompleted(page) }"
ng-click="setPageTemplateUrl(page)">
<span class="badge">{{ $index + 1 }}</span>
{{ page.title }}
<span class="chevron"></span>
</li>
</ul>
</div>
<div class="wizard-footer-right">
<button ng-disabled="nextShouldBeDisabled()"
ng-click="next()">
Next
</button>
</div>
</div>
</div>

Intro.html (first page in wizard)

<page>
<div class="page-title">{{ title }}</div>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. 
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu,
consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus.
Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet.
Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
</p> </page>

JavaScript

        (function ()
{
"use strict";
var app = angular.module("spike", ["ngAnimate"]);
}());
(function ()
{
"use strict";
var app = angular.module("spike");
var controller = function ($scope)
{
console.log("Controller [main] loaded.");
$scope.title = "Dynamic loading of views as directives in a AngularJS wizard.";
};
app.controller("main", ["$scope", controller]);
}());
(function ()
{
"use strict";
var directive = function ()
{
function link($scope, element, attributes, controller)
{
function initialize()
{
showPage(0);
}
function showPage(pageId)
{
$scope.selectedPageId = pageId;
var page = $scope.pages[$scope.selectedPageId];
$scope.setPageTemplateUrl(page);
}
$scope.chartData = [10, 20, 30, 40, 60, 80, 20, 50];
$scope.pages = [
{ 
title: "intro"
},
{
title: "modules"
},
{
title: "detail"
},
{
title: "order"
}
];
$scope.isActive = function (page)
{
var selectedPage = $scope.pages[$scope.selectedPageId];
return (selectedPage.title === page.title);
};
$scope.isCompleted = function (page)
{
var index = $scope.pages.indexOf(page);
return (index < $scope.selectedPageId);
};
$scope.next = function ()
{
showPage($scope.selectedPageId + 1);
};
$scope.prev = function ()
{
showPage($scope.selectedPageId - 1);
};
$scope.nextShouldBeDisabled = function ()
{
return ($scope.selectedPageId >= $scope.pages.length - 1);
};
$scope.prevShouldBeDisabled = function ()
{
return ($scope.selectedPageId <= 0);
};
$scope.setPageTemplateUrl = function (page)
{
$scope.selectedPageId = $scope.pages.indexOf(page);
$scope.pageTemplateUrl = page.title + ".html";
};
initialize();
console.log("Directive [wizard] loaded.");
}
return {
restrict: "EA",
link: link,
templateUrl: "wizard.html"
};
};
angular.module("spike").directive("wizard", [directive]);
}());
(function ()
{
"use strict";
var directive = function ()
{
function link($scope, element, attributes, controller)
{
var page = $scope.pages[$scope.selectedPageId];
$scope.title = page.title;
console.log("Directive [page].[" + page.title + "] loaded.");
}
return {
restrict: "EA",
link: link
};
};
angular.module("spike").directive("page", [directive]);
}());
(function ()
{
"use strict";
var directive = function ()
{
function link($scope, element, attributes, controller)
{
var chart = d3.select(element[0]);
chart.append("div").attr("class", "chart")
.selectAll('div')
.data($scope.data).enter().append("div")
.transition().ease("elastic")
.style("width", function (d) { return d + "%"; })
.text(function (d) { return d + "%"; });
console.log("Directive [barsChart] loaded.");
}
return {
restrict: "EA",
replace: false,
scope: {data: '=chartData'},
link: link
};
};
angular.module("spike").directive("barsChart", [directive]);
}());

One Comment

  1. Vitor

    Hi,

    Can you show how to make this views modal on button click, please.

    Anyway to allow move forward if you have already visited those views?

    Many thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *