gpt4 book ai didi

javascript - AngularJS ngSubmit 不调用 Controller 函数

转载 作者:行者123 更新时间:2023-11-30 12:37:52 25 4
gpt4 key购买 nike

我有一个表单,我正在尝试使用 ng-submit,但它没有调用 Controller 函数 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一切似乎都一样。

我尝试过的:

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

最佳答案

你的参数在你的 Controller 的构造函数中是错误的。你有:

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

将其更改为:

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

关于javascript - AngularJS ngSubmit 不调用 Controller 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com