无法在AngularJS中提交此表单?

时间:2016-04-06 23:01:41

标签: angularjs

我的当前代码没有错误。

var app = angular.module('mgcrea.ngStrapDocs', ['ngAnimate', 'ngSanitize', 'mgcrea.ngStrap']);

app.controller('MainCtrl', function($scope) {
});

angular.module('mgcrea.ngStrapDocs')
 .controller('NavbarDemoCtrl', function($scope, $location) {
   $scope.$location = $location;

 });

app.controller ('formController',[ '$scope', '$http', function($scope, $http) {
    $scope.formData = {};
    $scope.processForm = function () {
      console.log('Click');
      $http({
            method : 'POST',
            url    : '/users/login',
            data   : $scope.formData
        }).success(function(data, status, headers, config) {
            console.log(data);
        }).error(function() {
            console.log("ERROR")
        });
   }
}]);
use strict;

我确认url:localhosts / users / login通过Postman接受发布数据。但是,将所有内容组合在一起我发现我实际上无法将表单提交给api。我不确定是否存在一些冲突的代码或具体是什么问题,因为我之前没有提到任何错误。

此外,如果js似乎没有问题,这里是html的粘贴bin链接。

1 个答案:

答案 0 :(得分:0)

问题似乎出现在您的标记中。有几件事需要解决。

第一种是通过ng-app指令指定用于应用程序的Angular模块的名称。在您的情况下,这似乎是&#34; mgcrea.ngStrapDocs&#34;。因此,您的<html>代码变为:

<html ng-app="mgcrea.ngStrapDocs">

其次,你并没有告诉Angular在你的标记中使用你的控制器。在您的情况下,最简单的方法是使用ng-controller标记上的<form>指定它:

<form ng-controller="formController" ng-submit="processForm()">