简体   繁体   中英

AngularJS button submit not working

I am trying to call a Spring Restful service POST method from AngularJS.

Below is my JSP.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js"></script>

<script type="text/javascript">
var app = angular.module("UserManagement", []);
app.controller("UserManagementController", function($scope, $http) {

                //Initialize page with default data which is blank in this example
                $scope.policy = [];
                $scope.form = {
                    id : -1,
                    firstName : "",
                    lastName : "",
                    email : ""
                };

                //HTTP DELETE- delete employee by Id
                $scope.submitEmployee = function(policy) {
                    $http({
                        method : 'POST',
                        url : '/Add_Policy',
                        data : angular.toJson($scope.form),
                        headers : {
                            'Content-Type' : 'application/json'
                        }
                    }).then(_success, _error);
                };


            } );
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MMS Ensure</title>
</head>
<body ng-app="UserManagement"
    ng-controller="UserManagementController">
    <h1>New Policy</h1>
    <!-- https://dzone.com/articles/building-rest-service-collects -->
    <!-- http://howtodoinjava.com/angularjs/angularjs-http-restful-api-example/ -->
    <from ng-submit="submitEmployee()">
    <table>
        Policy #:
        <input type="text" ng-model="Policy">
        <br> Policy Type:
        <input type="text" ng-model="Type">
        <br> Policy Tenture
        <input type="text" ng-model="Tenture">
        <br> Start Date:
        <input type="text" ng-model="SDate">
        <br> Holder Name:
        <input type="text" ng-model="HName">
        <br> Age:
        <input type="text" name="Age">
        <br>
        <input type="submit">
    </table>
    </from>

</body>
</html>

If i press Submit button, its not calling my Service. Please help

More Info: I am running this locally and in the same project Spring service also present. I tried to acess the $scope in browser console, it was throwing error saying its undefined.

You made typo, its form element not from

<from ng-submit="submitEmployee()">

should be

<form ng-submit="submitEmployee()">

Other thing is technically table can not have other element inside it directly expect tbody , tr , tfoot , thead , td , etc(they can be lie inside td / th ). The current HTML which you had is wrong & invalid.

If you do inspect element you will find that the html which is there inside table must be thrown outside table element.

I don't see any reason you need table element here. You can simply remove table element wrapper.

There's typo , it should be

<form ng-submit="submitEmployee()">
<table>
    <thead>
        <tr>
            <td>
            </td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>
            </td>
        </tr>
    </tfoot>
</table>

and it's good pratice to keep your controller and html in seperate file.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM