AngularJS ngSubmit没有调用控制器功能

时间:2014-08-25 19:10:53

标签: javascript angularjs ng-submit

我有一个表单,我尝试使用ng-submit,但它没有调用控制器函数submit()

的index.html

...
<body ng-app="app">

    <div ng-view></div>
    <!-- In production use:
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
    -->
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="js/services.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/filters.js"></script>
    <script src="js/directives.js"></script>
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/app.js"></script>
</body>

表格:

<div class="container">
    <form class="form-signin" name="login" id="loginForm" ng-submit="submit()">
        <h2 class="form-signin-heading">Please sign in</h2>
        <input type="text" class="input-block-level" placeholder="Email Address" name="email" ng-model="formData.email">
        <input type="password" class="input-block-level" placeholder="Password"  name="password" ng-model="formData.password">
        <label class="checkbox">
            <input type="checkbox" value="remember-me"> Remember Me
        </label>
        <input class="btn btn-large btn-primary" type="submit">
    </form>
</div>

我的app.js:

'use strict';
var app = angular.module('app',['ngRoute', 'app.controllers']);

app.config(function($routeProvider) {
    $routeProvider.when('/', {
            templateUrl : 'partials/login.html',
            controller: 'loginController'});
});

我的controller.js:

'use strict';

/* Controllers */

angular.module('app.controllers', [])

.controller('loginController', ['$http', function($scope, $http) {
    $scope.formData = {};
    console.log('Controller Loaded');
    $scope.submit = function() {
        console.log('Click');
        $http({
            method: 'POST',
            url: 'http://localhost:8080/RoommateAPI/authentication/login',
            data: $scope.formData,
            headers: {'Content-Type': 'application/json'}
        }).success(function(data, status, headers, config) {
            console.log(data);
        }).error(function() {
            console.log("ERROR")
        });
    }
}]);

当我点击提交时,它没有错误或做任何事情......感觉我错过了一些明显但我已经看过this并且一切似乎都是同样的。

我尝试过的事情:

  • 将控制器整合到app.js
  • 从标记中删除了控制器的双重声明

2 个答案:

答案 0 :(得分:3)

您的参数在控制器的构造函数中是错误的。你有:

app.controller('loginController', ['$http', function($scope, $http) {

将此更改为:

app.controller('loginController', ['$scope', '$http', function($scope, $http) {

答案 1 :(得分:0)

尝试将表单标记放在div中,然后在此div而不是表单上声明控制器。