I am needing to do the following activities from my HTML page:
- User enters email and password to register
- Form is sent to Controller when user clicks
Submit - Control uses AJAX to create JSON request to RESTful Server and the server responds accordingly.
- According to Server's response, User should get alert window, and a
message printed on the current page (
register.html) below "Submit". If it is a "Success - Registered " or "Failed - Could not register".
But, currently it is working like this: 1), 2), 3) working as expected.
The user gets the alert window on first clicking
Submitwith the appropriate message.The user gets the message printed ONLY ON CLICKING
SubmitAGAIN, but once again, this is showing the alert window.
If I remove alert('Something') from JS, I have to click twice for the message to print on the register.html
Also, I want to bring it to your attention that clicking twice is also making server call twice. It behaves as if it has paused after the server call, and then to print the message I am supposed to click on Submit.
register.html
<div class='container'>
<div class='col-md-12'>
<div class='fill' ng-controller="registerController">
<form name="userForm" ng-submit="submitForm(userForm.$valid,user)" novalidate>
<!-- ALL FORM ELEMENTS HERE -->
<button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid" ng-click="showme=true">
Submit
</button>
<div class="wrapper">
<h3 ng-show="showme"><em>{{msgalert}}</em></h3>
</div>
</form>
</div>
</div>
</div>
My JS Controller looks like this stackoverflow_q3.js
// create angular controller
var register = angular.module('Main-app');
register.controller('registerController', function ($scope, $http, $location) {
// function to submit the form after all validation has occurred
$scope.submitForm = function (isValid, user) {
console.log('Stackoverflow JS func caled');
var regData = {
"email": user.email,
"password": user.password1
};
var jsonData = JSON.stringify(regData);
var request = $.ajax({
url: 'myurl',
type: 'POST',
data: jsonData,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
dataType: 'json',
complete: function (response) {
console.log(response.responseText);
if (response.responseText == 'success') {
console.log('Registration Success');
alert('Success');
$scope.msgalert = 'Registration Success, Proceed to Login and Continue';
} else if (response.responseText == 'fail') {
alert('registration failed');
$scope.msgalert = 'Registration Failed, Please try again';
}
}
});
};
});
Also in index.html I have mentioned the controller:
<!-- HTML -->
<!DOCTYPE html>
<html ng-app="Main-app">
<!-- Controllers -->
<script src="js/controllers/stackoverflow_q3.js" type="text/javascript"></script>
<!-- Other elements -->
</html>
Any idea how can I do all these activities in just one "Submit" click?
ng-show=showme, tryng-show=msgalert.$httpfunction, but using jQuery'sajaxfunction? Generally speaking, you'd want to create a service in angular and call that in your controller.