[英]Angular - Form won't submit
I seem to be overlooking something simple here but it has me stumped. 我似乎在这里忽略了一些简单的东西,但它让我难过。
Why does nothing happen when i hit the submit button? 当我点击提交按钮时为什么没有发生?
<section ng-controller="SavingsController as savingsCTRL">
<form name="createSavingForm" class="form-horizontal" novalidate>
<fieldset>
<!-- Title Box Start-->
<div class="form-group new-deal-form" show-errors>
<label for="title">Title</label>
<input name="title" type="text" ng-model="savingsCTRL.title" id="title" class="form-control" placeholder="Title" required>
<div class="sub-label">Enter the Title of the Deal.</div>
<div ng-messages="savingForm.savingsCTRL.title.$error" role="alert">
<p class="help-block error-text" ng-message="required">Saving title is required.</p>
</div>
</div>
<!-- Title Box End-->
<!--Submit Button Start-->
<div class="form-group buttons-cancel-submit">
<button class="btn btn-default " ng-click="savingsCTRL.cancel()">Cancel</button>
<input type="submit" class="btn btn-success " ng-click="savingsCTRL.create(); submitForm(createSavingForm.$valid)" >
</div>
</fieldset>
</form>
</div>
</div>
</section>
for simplicity i took most of the forms out but what else is wrong? 为简单起见,我把大部分表格都拿走了,但还有什么不对?
Savings Controller Function 节省控制器功能
// Create new Saving
$scope.create = function () {
$scope.error = null;
alert("create");
// Create new Saving object
var saving = new Savings({
title: this.title,
details: this.details,
retailer: this.retailer,
price: this.price,
link: this.link,
image: $scope.user.imageURL,
urlimage: this.urlimage,
tags: this.tags
//startdate: this.startdate,
//enddate: this.enddate
});
// Redirect after save
saving.$save(function (response) {
$location.path('savings/' + response._id);
// Clear form fields
$scope.title = '';
$scope.details = '';
$scope.retailer = '';
$scope.price = '';
$scope.link = '';
$scope.image = '';
$scope.urlimage = '';
$scope.tags = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};
Main issue is, you are mixing controller as
syntax with $scope
. 主要问题是,您将
controller as
与$scope
混合controller as
语法。
According to documentation , we should use this
instead of $scope
. 根据文档 ,我们应该使用
this
而不是$scope
。
... binds methods and properties directly onto the controller using
this
:ng-controller = "SettingsController1 as settings"
...使用
this
方法将方法和属性直接绑定到控制器上:ng-controller = "SettingsController1 as settings"
Than, submitForm
is not a predefined method, it should be defined in controller first 比,
submitForm
不是预定义的方法,它应该首先在控制器中定义
this.submitForm = function(isValid){
console.log('Submitting form: ' + isValid)
}
In addition to that, bind that to form with ng-submit= "savingsCTRL.submitForm(createSavingForm.$valid)"
除此之外,使用
ng-submit= "savingsCTRL.submitForm(createSavingForm.$valid)"
将其绑定到表单
See Plunker , with working code. 请参阅Plunker ,其中包含工作代码。 (I took ng-click="savingsCTRL.create()", since we don't have all parts of your application)
(我使用了ng-click =“savingsCTRL.create()”,因为我们没有应用程序的所有部分)
Bind the form submit event to ng-submit
. 将表单提交事件绑定到
ng-submit
。
Example: ng-submit="submitForm(createSavingForm.$valid)"
示例:
ng-submit="submitForm(createSavingForm.$valid)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.