This is the continuation from AngularJS Recipe: Part 4.

# Modules in AngularJS

Here in this example I am showing the model and controller in separate files.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AngularJS_Demo.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  7. <script src="myApp.js"></script>
  8. <script src="employeeController.js"></script>
  9. </head>
  10. <body>
  11. <div ng-app="myApp" ng-controller="employeeController">
  12. <table style="border: 4px solid red; background-color: skyblue; color: blue;" border="1">
  13. <tr ng-repeat="x in Emp_Names">
  14. <td style="width: 200px;">{{ x.name }}</td>
  15. <td style="width: 200px;">{{ x.country }}</td>
  16. </tr>
  17. </table>
  18. </div>
  19. </body>
  20. </html>
employeeController.js
  1. app.controller("employeeController", function ($scope) {
  2. $scope.Emp_Names = [
  3. { name: 'Shambhu Sharma', country: 'USA' },
  4. { name: 'Rahul Saxena', country: 'India' },
  5. { name: 'Abhishek Nigam', country: 'USA' },
  6. { name: 'Shraddha Gaur', country: 'India' },
  7. { name: 'Shweta Kashyap', country: 'India' },
  8. { name: 'Yogesh Gupta', country: 'USA' },
  9. { name: 'Rakesh Dixit', country: 'India' },
  10. { name: 'Manu Khanna', country: 'India' },
  11. { name: 'Saurabh Mehrotra', country: 'USA' },
  12. { name: 'Mayank Dhulekar', country: 'USA' },
  13. { name: 'Akhilesh Atwal', country: 'India' }
  14. ];
  15. });
myApp.js
  1. var app = angular.module("myApp", []);


# Making a Registration Form with input text and button
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AngularJS_Demo.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  7. <script>
  8. var app = angular.module('myApp', []);
  9. app.controller('formCtrl', function ($scope) {
  10. $scope.master = { Name: "Rahul Saxena", Email: "[email protected]", Password: "PWD", City: "Noida", Country: "India", Mobile: "958100000" };
  11. $scope.reset = function () {
  12. $scope.user = angular.copy($scope.master);
  13. };
  14. $scope.reset();
  15. });
  16. </script>
  17. </head>
  18. <body style="background-color: skyblue; color: blue; font-family: Arial; font-size: 12pt; font-weight: bold;">
  19. <div ng-app="myApp" ng-controller="formCtrl">
  20. <form novalidate>
  21. <table>
  22. <tr>
  23. <td>Name:
  24. </td>
  25. <td></td>
  26. <td>
  27. <input type="text" ng-model="user.Name"></td>
  28. </tr>
  29. <tr>
  30. <td>Email:
  31. </td>
  32. <td></td>
  33. <td>
  34. <input type="text" ng-model="user.Email"></td>
  35. </tr>
  36. <tr>
  37. <td>Password:
  38. </td>
  39. <td></td>
  40. <td>
  41. <input type="text" ng-model="user.Password"></td>
  42. </tr>
  43. <tr>
  44. <td>City:
  45. </td>
  46. <td></td>
  47. <td>
  48. <input type="text" ng-model="user.City"></td>
  49. </tr>
  50. <tr>
  51. <td>Country:
  52. </td>
  53. <td></td>
  54. <td>
  55. <input type="text" ng-model="user.Country"></td>
  56. </tr>
  57. <tr>
  58. <td>Mobile:
  59. </td>
  60. <td></td>
  61. <td>
  62. <input type="text" ng-model="user.Mobile"></td>
  63. </tr>
  64. <tr>
  65. <td></td>
  66. <td></td>
  67. <td>
  68. <button ng-click="reset()">RESET</button></td>
  69. </tr>
  70. </table>
  71. </form>
  72. <p>Current Form Value = {{user}}</p>
  73. <p>Master Value = {{master}}</p>
  74. </div>
  75. </body>
  76. </html>




# Creating a Registration Form with Validation
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AngularJS_Demo.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  7. <script>
  8. var app = angular.module('myApp', []);
  9. app.controller('validateCtrl', function ($scope) {
  10. $scope.user = 'Rahul Saxena';
  11. $scope.email = '[email protected]';
  12. $scope.password = 'PWD';
  13. $scope.city = 'Noida';
  14. $scope.country = 'India';
  15. $scope.mobile = '9581000000';
  16. });
  17. </script>
  18. </head>
  19. <body style="background-color: skyblue; color: blue; font-family: Arial; font-size: 12pt; font-weight: bold;">
  20. <form ng-app="myApp" ng-controller="validateCtrl"
  21. name="myForm" novalidate>
  22. <table>
  23. <tr>
  24. <td>Name:
  25. </td>
  26. <td></td>
  27. <td>
  28. <input type="text" name="user" ng-model="user" required>
  29. <span style="color: red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
  30. <span ng-show="myForm.user.$error.required">Username is required.</span>
  31. </span>
  32. </td>
  33. </tr>
  34. <tr>
  35. <td>Email:
  36. </td>
  37. <td></td>
  38. <td>
  39. <input type="email" name="email" ng-model="email" required>
  40. <span style="color: red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
  41. <span ng-show="myForm.email.$error.required">Email is required.</span>
  42. <span ng-show="myForm.email.$error.email">Invalid email address.</span>
  43. </span>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td>Password:
  48. </td>
  49. <td></td>
  50. <td>
  51. <input type="text" name="password" ng-model="password" required>
  52. <span style="color: red" ng-show="myForm.password.$dirty && myForm.password.$invalid">
  53. <span ng-show="myForm.password.$error.required">Password is required.</span>
  54. </span>
  55. </td>
  56. </tr>
  57. <tr>
  58. <td>City:
  59. </td>
  60. <td></td>
  61. <td>
  62. <input type="text" name="city" ng-model="city" required>
  63. <span style="color: red" ng-show="myForm.city.$dirty && myForm.city.$invalid">
  64. <span ng-show="myForm.city.$error.required">City is required.</span>
  65. </span>
  66. </td>
  67. </tr>
  68. <tr>
  69. <td>Country:
  70. </td>
  71. <td></td>
  72. <td>
  73. <input type="text" name="country" ng-model="country" required>
  74. <span style="color: red" ng-show="myForm.country.$dirty && myForm.country.$invalid">
  75. <span ng-show="myForm.country.$error.required">Country is required.</span>
  76. </span>
  77. </td>
  78. </tr>
  79. <tr>
  80. <td>Mobile:
  81. </td>
  82. <td></td>
  83. <td>
  84. <input type="text" name="mobile" ng-model="mobile" required>
  85. <span style="color: red" ng-show="myForm.mobile.$dirty && myForm.mobile.$invalid">
  86. <span ng-show="myForm.mobile.$error.required">Mobile is required.</span>
  87. </span>
  88. </td>
  89. </tr>
  90. <tr>
  91. <td></td>
  92. <td></td>
  93. <td>
  94. <input type="submit"
  95. ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
  96. myForm.email.$dirty && myForm.email.$invalid ||
  97. myForm.password.$dirty && myForm.password.$invalid ||
  98. myForm.city.$dirty && myForm.city.$invalid ||
  99. myForm.country.$dirty && myForm.country.$invalid ||
  100. myForm.mobile.$dirty && myForm.mobile.$invalid" />
  101. </td>
  102. </tr>
  103. </table>
  104. </form>
  105. </body>
  106. </html>