I have html form and angular controller. In controller I have one function to check user data on the server. and I call this function in button at html by using
ng-click
after success , I want submit form from controller.
How can I access form.submit
from angular controller ?
Thank !!!
I suggest you to use ng-submit directive on your form. From there you may return true/false after your validation.
Controller
app.controller('mainController', function($scope) {
$scope.submit = function(user) {
var isvalid = true;
// Add your code
// for server validation here
if (isvalid) {
return true;
}
return false; //failed
}
});
Html
<form name="userForm" ng-submit="submit(user)">
<input type="text" ng-model="user.email" />
<input type="text" ng-model="user.password" />
<button type="submit">Submit</button>
</form>
Suppose on ng-click
you call,
validate();
In this function u can do validation and form submission action as follows( my syntax may not be correct....but I'am just trying to convey my idea)
$scope.vaidate = function(input) {
//Add your validation code here, it includes api call etc.
// If validation succeeds, run code for submitting the form.
if() // add validation success condition
{
//run code for submitting the form.
}
else
{
// Add code to show error message within the html form.
}
}
I think you can run submit action within the if condition.
I have fixed it by using jquery to select form with method submit
$("#myForm").submit();
It work fine for me !!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.