I am new to AngularJS and I am trying the below sample code to submit my form to the server.
<!doctype html>
<html lang=''>
<head>
<meta charset="utf-8">
<script src="../js/angular.min.v1.5.5.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myController', ['$location', function($scope, $location) {
console.log(" controller invoked ** ");
$scope.submit = function(emp) {
var isvalid = true;
if (isvalid) {
$http.put('/addEmployee', {}).then(function(result) {
$location.path(result.data);
});
return true;
}
return false;
}
}]);
</script>
</head>
<body ng-app="myApp" ng-controller="myController">
<form name="form1" ng-submit="form1.submit(emp)">
<input type="text" ng-model="emp.name" />
<div align='center'>
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>
I have started debugging the issue, I have noticed that "controller invoked **" printed once during the form loading, but not after the form submition.
Could you please help by suggesting the changes required to submit the form ?
Thank you.
Form object doesn't have submit
method as it is available there in $scope
, you need to call it directly. Instead of doing form1.submit(emp)
on ng-submit
directive, do change it to below.
ng-submit="submit(emp)"
Additionally do correct mistake in controller DI array
app.controller('myController', ['$location',
function($scope, $location) {
should be
app.controller('myController', ['$scope', '$location', //<-- added missing $scope dependency here
function($scope, $location) {
Pass data to put method in its 2nd parameter, currently you are passing as {}
blank object just change it to emp
will do the trick
$http.put('/addEmployee', emp).then(function(result) {
$location.path(result.data);
});
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.