AngularJS Step by Step Form using ngRoute

Getting Started

I've used CDN for Bootstrap, Angular JS, Angular Animate and Angular Router so you need internet connection for them to work.

index.html

This is the main view of our app.
  1. <!DOCTYPE html>
  2. <html ng-app="myapp">
  3. <meta charset="utf-8">
  4. <title>AngularJS Step by Step Form</title>
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-animate.min.js"></script>
  8. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular-route.min.js"></script>
  9. <link rel="stylesheet" href="style.css">
  10. </head>
  11. <div class="container" ng-controller="mainCtrl">
  12. <h1 class="page-header text-center">AngularJS Step by Step Form</h1>
  13. <h3 class="text-center">Feedback Form</h3>
  14. <div class="stepindicator">
  15. <span ng-class="active('/')"></span>
  16. <span ng-class="active('/second')"></span>
  17. <span ng-class="active('/third')"></span>
  18. </div>
  19. <div class="row">
  20. <div class="col-sm-4 col-sm-offset-4">
  21. <div class="panel panel-default">
  22. <div class="panel-body">
  23. <div id="myview" ng-view></div>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <div class="row">
  29. <div class="col-sm-6 col-sm-offset-3">
  30. {{ feedback }}
  31. </div>
  32. </div>
  33. </div>
  34. <script src="app.js"></script>
  35. </body>
  36. </html>
In here we have set up our step indicator for users to determine the steps and I've put the value of the users input at the bottom.

style.css

Next, we add the ff styles for our form and our animation.
  1. /*Form Styles*/
  2. .stepindicator {
  3. text-align: center;
  4. }
  5.  
  6. .step {
  7. height: 15px;
  8. width: 15px;
  9. margin: 0 2px;
  10. background-color: #bbbbbb;
  11. border: none;
  12. border-radius: 50%;
  13. display: inline-block;
  14. opacity: 0.5;
  15. }
  16.  
  17. .step.active {
  18. opacity: 1;
  19. }
  20.  
  21. .panel {
  22. position:relative;
  23. min-height:230px;
  24. overflow:hidden;
  25. margin-top: 10px;
  26. }
  27.  
  28. .disabled {
  29. cursor: default; opacity: 0;
  30. }
  31.  
  32. /*Animations*/
  33. #myview.ng-enter,
  34. #myview.ng-leave {
  35. position:absolute; left:30px; right:30px;
  36. transition:0.5s all ease; -moz-transition:0.5s all ease; -webkit-transition:0.5s all ease;
  37. }
  38.  
  39. #myview.ng-enter {
  40. -webkit-animation:slideInRight 0.5s both ease;
  41. -moz-animation:slideInRight 0.5s both ease;
  42. animation:slideInRight 0.5s both ease;
  43. }
  44.  
  45. #myview.ng-leave {
  46. -webkit-animation:slideOutLeft 0.5s both ease;
  47. -moz-animation:slideOutLeft 0.5s both ease;
  48. animation:slideOutLeft 0.5s both ease;
  49. }
  50.  
  51. @keyframes slideOutLeft {
  52. to { transform: translateX(-200%); }
  53. }
  54. @-moz-keyframes slideOutLeft {
  55. to { -moz-transform: translateX(-200%); }
  56. }
  57. @-webkit-keyframes slideOutLeft {
  58. to { -webkit-transform: translateX(-200%); }
  59. }
  60.  
  61. @keyframes slideInRight {
  62. from { transform:translateX(200%); }
  63. to { transform: translateX(0); }
  64. }
  65. @-moz-keyframes slideInRight {
  66. from { -moz-transform:translateX(200%); }
  67. to { -moz-transform: translateX(0); }
  68. }
  69. @-webkit-keyframes slideInRight {
  70. from { -webkit-transform:translateX(200%); }
  71. to { -webkit-transform: translateX(0); }
  72. }

app.js

This contains our angular js scripts.
  1. var myapp = angular.module('myapp', ['ngRoute', 'ngAnimate']);
  2.  
  3. myapp.config(function($routeProvider){
  4. $routeProvider
  5. .when('/', {
  6. templateUrl: 'first.html'
  7. })
  8. .when('/second', {
  9. templateUrl: 'second.html'
  10. })
  11. .when('/third', {
  12. templateUrl: 'third.html'
  13. })
  14. .otherwise({
  15. redirectTo: '/'
  16. });
  17. });
  18.  
  19. myapp.controller('mainCtrl', function($scope, $location){
  20. $scope.feedback = {};
  21.  
  22. $scope.submit = function(){
  23. alert('Form Submitted!');
  24. $location.path('/');
  25. $scope.feedback = {};
  26. }
  27.  
  28. $scope.active = function(path){
  29. return ($location.path() === path) ? 'active step' : 'step';
  30. }
  31.  
  32. //directions
  33. $scope.gotoFirst = function(){
  34. $location.path('/');
  35. }
  36. $scope.gotoSecond = function(){
  37. $location.path('/second');
  38. }
  39. $scope.gotoThird = function(){
  40. $location.path('/third');
  41. }
  42. });
In here, we have added the dependency for Angular Animate and Angular Route, we have set up our routes in .config and we set up our main controller. active function determines which step is currently active.

Creating our Steps Template

Lastly, create the ff templates that serves as our steps. first.html
  1. <form name="step1">
  2. <div class="form-group">
  3. <label>Firstname:</label>
  4. <input type="text" class="form-control" ng-model="feedback.firstname" required>
  5. </div>
  6. <div class="form-group">
  7. <label>Lastname:</label>
  8. <input type="text" class="form-control" ng-model="feedback.lastname" required>
  9. </div>
  10. <div class="col-sm-6 col-sm-offset-3">
  11. <button type="button" class="btn btn-primary btn-block" ng-disabled="step1.$invalid" ng-click="gotoSecond()">Next <span class="glyphicon glyphicon-arrow-right"></span></button>
  12. </div>
  13. </form>
second.html
  1. <form name="step2">
  2. <div class="form-group">
  3. <label>Adress:</label>
  4. <input type="text" class="form-control" ng-model="feedback.address" required>
  5. </div>
  6. <div class="form-group">
  7. <label>Email:</label>
  8. <input type="email" class="form-control" ng-model="feedback.email" required>
  9. </div>
  10. <button type="button" class="btn btn-success" ng-click="gotoFirst()"><span class="glyphicon glyphicon-arrow-left"></span> Previous</button>
  11. <button type="button" class="btn btn-primary" ng-disabled="step2.$invalid" ng-click="gotoThird()">Next <span class="glyphicon glyphicon-arrow-right"></span></button>
  12. </form>
third.html
  1. <form name="step3">
  2. <div class="form-group">
  3. <label>Message:</label>
  4. <textarea class="form-control" rows="4" ng-model="feedback.message" required></textarea>
  5. </div>
  6. <button type="button" class="btn btn-success" ng-click="gotoSecond()"><span class="glyphicon glyphicon-arrow-left"></span> Previous</button>
  7. <button type="button" class="btn btn-primary" ng-disabled="step3.$invalid" ng-click="submit()"><span class="glyphicon glyphicon-check"></span> Submit</button>
  8. </form>
Notice that I have used buttons in directions instead of using anchor tag. This is because I found an issue about anchor tag with ng-disable wherein even if disabled you can still click the anchor tag which is not supposed to be. That ends this tutorial. Happy Coding :)

Add new comment