Introduction

In my previous articles, I explained a lot about Angular.js; i.e., how to save, update, delete, get, paging searching, sorting etc. In this article, we talk about the new library of Angular.js; i.e, Angular material .

Here a question arises. What is Angular material and how to use it ?

Answer

Angular material is a component library of Angular.JS that is used to create attractive, responsive Web pages; that is the need of today's standards.

Angular material's built in responsive features helps us to create the Website irrespective of any devices. The Website runs on all the devices - either it is a desktop or a mobile. It automatically adjusts itself on the screen accordingly.

Angular material provides almost all types of the controls with better designs - either it is navigation bars or they are form inputs, everything is provided with Angular.js library.

sign in

Start creating the database

  • Create a database named Test.

  • Create a table Login in the database.

    table

Start with code

Basic setup

  • Open VS and create a new project.

  • Go to the reference folder in Solution Explorer and right click on the folder, select Manage NuGet Packages.

  • Install the Angular material and Angular.JS libraries online or download from the links, given below:

  • Open BundleConfig.cs file in App_Start folder.

  • Remove the code inside from RegisterBundles method. Don't remove the method.

Layout setup of the project

  • Open the Layout.cshtml file in shared folder of views.

  • Start writing code, as shown below:
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="utf-8" />
    5. <title>@ViewBag.Title</title>
    6. <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    7. <meta name="viewport" content="width=device-width" />
    8. <link href="~/Content/angular-material.min.css" rel="stylesheet" />
    9. <script src="~/Scripts/angular.min.js"></script>
    10. <script src="~/Scripts/angular-animate/angular-animate.min.js"></script>
    11. <script src="~/Scripts/angular-aria/angular-aria.min.js"></script>
    12. <script src="~/Scripts/angular-messages.min.js"></script>
    13. <script src="~/Scripts/angular-material/angular-material.min.js"></script>
    14. </head>
    15. <body>
    16. <div class="wrapper">
    17. <div class="container">
    18. @RenderBody()
    19. </div>
    20. </div>
    21. @RenderSection("scripts", required: false)
    22. </body>
    23. </html>
  • Here, in the head section, drag and drop the files whose links are given above.

UI Creation

  • Go to the Controller folder, create Register Controller.

  • Change the name of the Action Result index to register.

  • Right click on Action Result register and select addview option.

  • Go to the Register view that is created from register actionresult.

    Start writing code
    1. <html ng-app="myApp">
    2. <head>
    3. <title>Register</title>
    4. </head>
    5. <body>
    6. <div ng-controller="controll" layout="column" layout-padding ng-cloak>
    7. <br />
    8. <md-content class="md-no-momentum">
    9. <md-toolbar class="md-accent">
    10. <h2 class="md-toolbar-tools">
    11. <span flex>Register User</span>
    12. </h2>
    13. </md-toolbar><br /><br />
    14. <form name="userForm">
    15. <div layout-gt-sm="column" flex="30">
    16. <md-input-container class="md-icon-float md-block">
    17. <label>First Name</label>
    18. <md-icon md-svg-src="~/Content/Icons/employee.svg" class="fName"></md-icon>
    19. <input type="text" class="form-control" name="fName" ng-model="fName" required autofocus />
    20. <div ng-messages="userForm.fName.$error">
    21. <div ng-message="required">First name is required!</div>
    22. </div>
    23. </md-input-container>
    24. <md-input-container class="md-icon-float md-block">
    25. <label>Last Name</label>
    26. <md-icon md-svg-src="~/Content/Icons/employee.svg" class="lName"></md-icon>
    27. <input type="text" class="form-control" name="lName" ng-model="lName" required autofocus />
    28. <div ng-messages="userForm.lName.$error">
    29. <div ng-message="required">Last name is required !</div>
    30. </div>
    31. </md-input-container>
    32. </div>
    33. <div layout-gt-sm="column" flex="30">
    34. <md-input-container class="md-icon-float md-block">
    35. <label>Email</label>
    36. <md-icon md-svg-src="~/Content/Icons/placeholder.svg" class="uEmail"></md-icon>
    37. <input type="email" class="form-control" name="uEmail" ng-model="uEmail" ng-pattern="/^.+.+\..+$/" required autofocus />
    38. <div ng-messages="userForm.uEmail.$error">
    39. <div ng-message-exp="['required','pattern']">Email is required in correct format !</div>
    40. </div>
    41. </md-input-container>
    42. <md-input-container class="md-icon-float md-block">
    43. <label>Username</label>
    44. <md-icon md-svg-src="~/Content/Icons/placeholder.svg" class="uName"></md-icon>
    45. <input type="text" class="form-control" name="uName" ng-model="uName" md-maxlength="8" required autofocus />
    46. <div ng-messages="userForm.uName.$error">
    47. <div ng-message="required">Username is required !</div>
    48. <div ng-message="md-maxlength">Maximum 8 characters allowed !</div>
    49. </div>
    50. </md-input-container>
    51. </div>
    52. <div layout-gt-sm="column" flex="30">
    53. <md-input-container class="md-icon-float md-block" flex-gt-sm>
    54. <label>Password</label>
    55. <md-icon md-svg-src="~/Content/Icons/cityscape.svg" class="uPwd"></md-icon>
    56. <input type="password" class="form-control" name="uPwd" ng-model="uPwd" md-maxlength="10" required autofocus />
    57. <div ng-messages="userForm.uPwd.$error">
    58. <div ng-message="required">Password is required !</div>
    59. <div ng-message="md-maxlength">Maximum 10 characters allowed !</div>
    60. </div>
    61. </md-input-container>
    62. <md-input-container class="md-icon-float md-block" flex-gt-sm>
    63. <md-button class="md-raised md-primary" ng-click="SaveUser();" ng-disabled="!(fName && lName && uEmail && uName && uPwd)">
    64. <md-tooltip>
    65. Save
    66. </md-tooltip>
    67. Save
    68. </md-button>
    69. <md-button class="md-raised md-primary">
    70. <md-tooltip>
    71. Sign in
    72. </md-tooltip>
    73. Sign in</md-button>
    74. </md-input-container>
    75. </div>
    76. </form>
    77. </md-content>
    78. </div>
    79. </body>
    80. </html>
  • In the code shown above, every tag is prefixed with md and .md is a prefixed for Angular material design.

  • Let's start with each and every tag .

  • md-content is a tag, in which all the code resides. It is given with a class, that is defined in the Angular css file with certain designs.

  • md-toolbar is used to design a toolbar on the upper side of the page.

  • md-input-container is a Container for input like in the bootstrap, it contains class for display icon and other css effects of label, which is defined inside this.

  • md-icon is used to display the icon before the control and we have to give path of the icon in md-svg-src attribute of this tag.

  • ng-messages is used to display the error message for the control on violation of validation rules.

  • ng-message is used inside the ng-messages tag to display multiple messages.

  • Both of these are used with div tags.

  • Inside the ng-messages atribute, we have to give the model name of the field with check of an error.

  • Inside the ng-message attribute, the type of validation is declared. For ex.-- we declare, what is required in an input tag. This is used with ng-message to display the required message.

Create Scripts

  • Create Module.js file in the content folder of Solution Explorer.

  • Write the code, given below, in the file.
    1. var app = angular.module("myApp", ['ngMaterial','ngMessages']);
  • Here, in the code, we register the app with Angular.

  • ngMaterial directive is used to register with Angular material library and ngMessages are used to display the messages.

  • Create Controller.js file in the same folder.

    Start writing code
    1. app.controller("controll", function($scope, myService)
    2. {
    3. $scope.SaveUser = function()
    4. {
    5. var User =
    6. {
    7. FName: $scope.fName,
    8. LName: $scope.lName,
    9. Email: $scope.uEmail,
    10. Password: $scope.uPwd,
    11. UserName: $scope.uName
    12. };
    13. var response = myService.AddUser(User);
    14. response.then(function(data)
    15. {
    16. if (data.data == "1")
    17. {
    18. clearFields();
    19. alert("User Created !");
    20. window.location.href = "/Register/Login";
    21. } else if (data.data == "-1")
    22. {
    23. alert("user already present !");
    24. } else
    25. {
    26. clearFields();
    27. alert("Invalid data entered !");
    28. }
    29. });
    30. }
    31. function clearFields()
    32. {
    33. $scope.fName = "";
    34. $scope.lName = "";
    35. $scope.uEmail = "";
    36. $scope.uPwd = "";
    37. $scope.uName = "";
    38. $scope.userForm.$setPristine();
    39. }
    40. });
  • Here, in the Controller Register, the Controller with name controll helps to declare $scope and service.

  • Create SaveUser() function to get the data from the page and send it to the service for further processing.

  • AddUser() function from the service is called by passing data to the function.

  • clearFields() function is used to clear the textboxes.

  • Create Service.js file in the same folder.

    Write the code as given below
    1. app.service("myService", function($http)
    2. {
    3. this.AddUser = function(User)
    4. {
    5. var response = $http
    6. ({
    7. method: "post",
    8. url: "/Register/AddUser",
    9. data: JSON.stringify(User),
    10. dataType: "json"
    11. });
    12. return response;
    13. };
    14. });
  • Here, myService is the name with which the service is registered.

  • AddUser function calls the AddUser method from the Server.

  • Pass the correct URL in the URL Parameter.

Write Server code

  • Go to the Register controller, add method AddUser with string as a return type.

    See the code, shown below:
    1. public string AddUser(Login1 usr)
    2. {
    3. if (usr != null)
    4. {
    5. if (CheckUser(usr.UserName) == false)
    6. {
    7. using(SahilEntities context = new SahilEntities())
    8. {
    9. Login1 createUser = new Login1();
    10. createUser.UserName = usr.UserName;
    11. createUser.Fname = usr.Fname;
    12. createUser.Lname = usr.Lname;
    13. createUser.Email = usr.Email;
    14. createUser.DateTimeCreated = DateTime.Now;
    15. createUser.Password = usr.Password;
    16. context.Logins1.Add(createUser);
    17. context.SaveChanges();
    18. }
    19. return "1";
    20. } else
    21. {
    22. return "-1";
    23. }
    24. } else
    25. {
    26. return "0";
    27. }
    28. }
    29. public bool CheckUser(string user)
    30. {
    31. bool Exists = false;
    32. using(SahilEntities context = new SahilEntities())
    33. {
    34. var uName = context.Logins1.Where(x => x.UserName == user).ToList();
    35. if (uName.Count != 0)
    36. {
    37. Exists = true;
    38. }
    39. }
    40. return Exists;
    41. }
  • There are 2 methods; one is AddUser that returns response on the basis of execution.

  • Other is CheckUser that checks if the user already exists or not.

  • If a user already exists, an error displays, otherwise a user is created.

Including scripts in the view

  • Go to the Register.cshtml and include the Angular module, controller and service file in the head section of the page.

  • @section scripts are used to include the script files.
    1. <head>
    2. <title>Register</title>
    3. @section scripts
    4. {
    5. <script src="~/Content/Module.js"></script>
    6. <script src="~/Content/Service.js"></script>
    7. <script src="~/Content/Controller.js"></script>
    8. }
    9. </head>
  • mOpen the VS and create your Website with Angular.JS and give new attractive look with the Angular material.

Refrence articles