2016-01-20 137 views
0

我正在開發一個angular-js項目。我想通過ng-submit提交表單。但這不行。 我的HTML代碼angular ng-submit無法正常工作

<!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> 

我的表單模板

<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> 

/add/view ROURE做工精細。但是當我提交表格時,ng-submit不起作用。表單直接提交。我在控制檯中沒有得到任何東西。我的問題在哪裏?謝謝。

回答

2

嘗試intializing $ scope.form:

$scope.formData = {}; 
$scope.form = {}; 
+0

謝謝。我得到了我的問題,當我刪除'action =「#」'屬性時,一切都很好。 –

1
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); 
    } 
}); 
+0

謝謝。我得到了我的問題,當我刪除'action =「#」'屬性時,一切都很好。 –

0

基於您所提交的代碼,你不打算使用$scope.formData。我認爲它是你犯的一個錯字。只需將其改爲$scope.form = {}

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