1

I am currently working on Angular Js with Sql Database . I am trying to creating user registration system though Angular Js with Sql Database but when I click the submit button I got following error ...

angular.js:14642 TypeError: $ is not a function
    at b.$scope.SaveUser (Module.js:6)
    at fn (eval at compile (angular.js:15500), <anonymous>:4:144)
    at e (angular.js:27285)
    at b.$eval (angular.js:18372)
    at b.$apply (angular.js:18472)
    at HTMLInputElement.<anonymous> (angular.js:27290)
    at kg (angular.js:3771)
    at HTMLInputElement.d (angular.js:3759

Here is code in Register Controller...

 public class RegisterController : Controller
        {
            public ActionResult Register()
            {
                return View();
            }

            //To check that user entered is already present or not.  
            public bool CheckUser(string user)
            {
                bool Exists = false;
                using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
                {
                    var uName = context.UserLogins.Where(x => x.UserName == user).ToList();
                    if (uName.Count != 0)
                    {
                        Exists = true;
                    }
                }
                return Exists;
            }

            //For saving the user details in database table.          
            public string AddUser(UserLogin usr)
            {
                if (usr != null)
                {
                    if (CheckUser(usr.UserName) == false)
                    {
                        using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
                        {
                            UserLogin createUser = new UserLogin();
                            createUser.UserName = usr.UserName;
                            createUser.Firstname = usr.Firstname;
                            createUser.Lastname = usr.Lastname;
                            createUser.Email = usr.Email;
                            createUser.DateTimeCreated = DateTime.Now;
                            createUser.Password = Utility.Encryptpassword(usr.Password);
                            context.UserLogins.Add(createUser);
                            context.SaveChanges();
                        }
                        return "User created !";
                    }
                    else
                    {
                        return "User already present !";
                    }
                }
                else
                {
                    return "Invalid Data !";
                }
            }
        }
    }  

Here is my Code for Module.js ..

var app = angular.module("myApp", [])
    .controller('Ctrl', ['$scope', function ($scope) {

        $scope.SaveUser = function () {
            $("#divLoading").show();
            var User = {
                FName: $scope.fName,
                LName: $scope.lName,
                Email: $scope.uEmail,
                Password: $scope.uPwd,
                UserName: $scope.uName
            };

            var response = myService.AddUser(User);
            response.then(function (data) {
                if (data.data == "1") {
                    $("#divLoading").hide();
                    clearFields();
                    alert("User Created !");
                    window.location.href = "/Register/Login";
                }
                else if (data.data == "-1") {
                    $("#divLoading").hide();
                    alert("user alraedy present !");
                }
                else {
                    $("#divLoading").hide();
                    clearFields();
                    alert("Invalid data entered !");
                }
            });
        }

        function clearFields() {
            $scope.fName = "";
            $scope.lName = "";
            $scope.Email = "";
            $scope.Password = "";
            $scope.UserName = "";
        }

    }])
    .service("myService", function ($http) {

        this.AddUser = function (User) {
            var response = $http({
                method: "post",
                url: "/Register/AddUser",
                data: JSON.stringify(User),
                dataType: "json"
            });
            return response;
        }
    })

Here is my HTML CODE ...

@{
    Layout = null;
}


<html ng-app="myApp">
<head>
    <title>Register</title>

    <script src="~/Scripts/angular.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
   <script src="~/Scripts/MyScript/Module.js"></script>
    <link href="~/Content/bootstrap.css" rel="stylesheet" />


</head>
<body>

    <div class="container" ng-controller="Ctrl">
        <br />

        <div class="row">
            @*<img src="~/Content/Images/user.png" />*@<h4>Register User</h4>
            <hr />

            <br />
            <div class="col-md-6">
                <form name="userForm" novalidate>
                    <div class="form-horizontal">
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
                                    First Name :
                                </div>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" placeholder="First Name" name="fName" ng-model="fName" required autofocus />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
                                    Last Name :
                                </div>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" placeholder="Last Name" name="lName" ng-model="lName" required autofocus />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-3" style="margin-left: 15px; color: #5bc0de">
                                    Email :
                                </div>
                                <div class="col-md-6">
                                    <input type="email" class="form-control" placeholder="User's Email" name="uEmail" ng-model="uEmail" required autofocus />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
                                    Username :
                                </div>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" placeholder="Username" name="uName" ng-model="uName" required autofocus />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-3" style="margin-left: 15px; color: #5bc0de;">
                                    Password :
                                </div>
                                <div class="col-md-6">
                                    <input type="password" class="form-control" placeholder="Password" name="uPwd" ng-model="uPwd" required autofocus />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-4"></div>
                                <div class="col-md-3">
                                    <input type="button" value="Save" ng-click="SaveUser();" class="btn btn-success" />
                                </div>
                                <div class="col-md-3">
                                    @Html.ActionLink("Sign in", "Login", "Register", new { @class = "btn btn-info" })
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                                <div class="col-md-6">
                                    <div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001; opacity: .8; filter: alpha(opacity=70); display: none">
                                        <p style="position: absolute; top: 30%; left: 45%; color: White;">
                                            please wait...<img src="~/Content/images/load.png">
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html> 

Here is screen shot when I run the application Click Here to See the Out Put what is best solution to solved this problem ..Thanks

Rasel
  • 239
  • 4
  • 15

1 Answers1

3

You will need to include jquery in your scripts path.

Something like this:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

Make sure you add it before Module.js.

Also to make that function work you will need to add jquery-ui and bootstrap.js.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 1
    Angularjs does not come with jquery and you are using jquery to select the div in the 6th line of module.js – Pritam Banerjee Aug 28 '17 at 00:37
  • Give me a min I checking it now – Rasel Aug 28 '17 at 00:38
  • Module.js:56 Uncaught TypeError: angular.module(...).controller(...).service(...) is not a function at Module.js:56 (anonymous) @ Module.js:56 Register:96 GET http://localhost:53526/Content/images/load.png 404 (Not Found) jquery-ui.js:351 Uncaught ReferenceError: jQuery is not defined at jquery-ui.js:351 (anonymous) @ jquery-ui.js:351 – Rasel Aug 28 '17 at 00:41
  • Where and how did you add it? – Pritam Banerjee Aug 28 '17 at 00:43
  • According you suggestion – Rasel Aug 28 '17 at 00:44
  • @Rasel Try with this one: – Pritam Banerjee Aug 28 '17 at 00:46
  • I added it before Module.js in HTML Code ... what is other changes i have to do ???? – Rasel Aug 28 '17 at 00:46
  • @Rasel Try with the one I gave – Pritam Banerjee Aug 28 '17 at 00:47
  • i got more error ...like this .......Failed to load resource: the server responded with a status of 404 (Not Found) load.png Failed to load resource: the server responded with a status of 404 (Not Found) Register:1 Error parsing 'integrity' attribute ('sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt‌​4='). The digest must be a valid, base64-encoded value. jquery-3.2.1.min.js Failed to load resource: the server responded with a status of 404 (Not Found) Module.js:56 Uncaught TypeError: angular.module(...).controller(...).service(...) is not a function – Rasel Aug 28 '17 at 00:50
  • @Rasel Try with this one: – Pritam Banerjee Aug 28 '17 at 00:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152974/discussion-between-rasel-and-pritam-banerjee). – Rasel Aug 28 '17 at 00:54