简体   繁体   中英

angular ng-submit doesnot work

I am working on a angular-js project. I want to submit a form by ng-submit. But It is not work. My Html code is

<!DOCTYPE html>
<html lang="en">
<head>
    <title>AngularJS Routes example</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
</head>

<body ng-app="sampleApp" class="container">

<div class="btn-group">
    <a href="/view" class="btn btn-info">List</a>
<a href="/add" class="btn btn-danger">Add New</a>
</div>
<hr>

<div ng-view></div>

<script>
    var module = angular.module("sampleApp", ['ngRoute']);

    module.config(['$routeProvider','$locationProvider',
        function($routeProvider,$locationProvider) {
            $routeProvider.
                    when('/view', {
                        templateUrl: 'template/view/list',
                        controller: 'RouteController'
                    }).
                    when('/add', {
                        templateUrl: 'template/view/new',
                        controller: 'RouteController'
                    }).
                    otherwise({
                        redirectTo: '/view'
                    });
                    $locationProvider.html5Mode(true);
        }]);

    module.controller("RouteController", function($http,$scope, $routeParams) {
        $http.get('welcome/phones').success(function(data) {
          $scope.phones = data;
        });

        $scope.formData = {};

        $scope.saveInfo = function() {

            formData = $scope.form;
            console.log(formData);

            $http.post('welcome/save',formData);
        }
    });
</script>



</body>
</html> 

My form template is

<form action="#" ng-submit="saveInfo()" method="post" ng-controller="RouteController">
    <div class="form-group">
        <label for="">Product</label>
        <input type="text" name="name" ng-model="form.name" class="form-control">
    </div>
    <div class="form-group">
        <label for="">Price</label>
        <input type="text" name="price" ng-model="form.price" class="form-control">
    </div>
    <div class="form-group">
        <input type="submit" class="btn btn-info" value="Submit">
    </div>
</form>

My /add , /view roure work fine. But when I submit the form, ng-submit does not work. The form submit directly. I does not get any thing in console. Where is my problem?. Thank you.

Try intializing $scope.form:

$scope.formData = {};
$scope.form = {};
module.controller("RouteController", function($http,$scope, $routeParams) {
    $http.get('welcome/phones').success(function(data) {
      $scope.phones = data;
    });

    $scope.formData = {};
    $scope.form = {};
    $scope.saveInfo = function() {

         $scope.formData = $scope.form;
        console.log( $scope.formData);

        $http.post('welcome/save', $scope.formData);
    }
});

Based upon the code you have submitted, you don't intend to use $scope.formData . I think its a typo you made. Just change that like to $scope.form = {}

$scope.form = {}; // instead of $scope.formData

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