Add 3 files --> Module.js,Controller.js,Service.js in content folder
- Here we register the module with the application.
- Here myApp is the module name that we used with ng-app directive in HTML view.
- In Module.js write the given code.
- var app = angular.module("myApp", []);
- myCntrl is the controller name that is registered with the myApp module and used in HTML view with ng-controller directive.
- $scope is used to refers to the application.Data passed to $scope in controller is accessible in view.
- myService is the name of service that is used with controller to call the functions from server.
- LoginCheck function get the username and password from $scope and store in the object variables.
- UserLogin function from service is called that returns the response in form of values after that certain action performs.
- Clearfields clear the HTML controls after login.
- alertmsg hides the alert modal that displays alert messages.
- app.controller("myCntrl", function ($scope, myService) {
-
- $scope.LoginCheck = function () {
- var User = {
- UserName: $scope.uName,
- Password: $scope.password
- };
- $("#divLoading").show();
- var getData = myService.UserLogin(User);
- getData.then(function (msg) {
- if (msg.data == "0") {
- $("#divLoading").hide();
- $("#alertModal").modal('show');
- $scope.msg = "Password Incorrect !";
- }
- else if (msg.data == "-1") {
- $("#divLoading").hide();
- $("#alertModal").modal('show');
- $scope.msg = "Username Incorrect !";
- }
- else {
- uID = msg.data;
- $("#divLoading").hide();
- window.location.href = "/Home/Index";
- }
- });
- debugger;
- }
-
- function clearFields() {
- $scope.uName = '';
- $scope.uPwd = '';
- }
-
- });
- $scope.alertmsg = function () {
- $("#alertModal").modal('hide');
- };
- In Service.js write the given code.
- myService is the name of the service registers with the myApp module.
- $http is passed as parameter. $http serves the purpose for ajax call to the server.
- In this service UserLogin function is used to call the Login method from server by providing url.
- This method returns the response to the controller.
- app.service("myService", function ($http) {
-
- this.UserLogin = function (User) {
- var response = $http({
- method: "post",
- url: "/Login/Login",
- data: JSON.stringify(User),
- dataType: "json"
- });
- return response;
- }
-
- });
Now write the code to display the view on screen
Apply CSS and Scripts file
- Find BundleConfig.cs in App_Start folder.
- Here we register the css snd js files to use with the layout page.
- public static class BundleConfig
- {
- public static void RegisterBundles(BundleCollection bundles)
- {
- RegisterStyleBundles(bundles);
- RegisterJavascriptBundles(bundles);
- }
-
- private static void RegisterStyleBundles(BundleCollection bundles)
- {
- bundles.Add(new StyleBundle("~/css")
- .Include("~/Content/bootstrap.css")
- .Include("~/Content/carousel.css")
- .Include("~/Content/site.css"));
- }
-
- private static void RegisterJavascriptBundles(BundleCollection bundles)
- {
- bundles.Add(new ScriptBundle("~/js")
- .Include("~/Scripts/jquery-{version}.js")
- .Include("~/Scripts/jquery-ui-{version}.js")
- .Include("~/Scripts/bootstrap.js"));
- }
- }
Start by creating your own app.
Read more articles on AngularJS: