我在angularjs中有一个表单
<form ng-submit="addNewNote(noteData)">
<button type="submit" class="md-icon-button ion-nav-button-right md-button md-default-theme" aria-label="Setting" tabindex="0">
{{noteTxt}}
</button>
<ion-content>
<div id="note-detail-content">
<md-input-container md-no-float>
<input required name="noteTitle" placeholder="Note Title (Required)" ng-model="noteData.title">
</md-input-container>
<textarea rows="7" ng-model="noteData.description" maxlength="250"
placeholder="Write something here ...."></textarea>
<span>
</span>
</div><!--end content section-->
</ion-content>
<!--end note detail section-->
</form>
问题是ng-submit
被触发两次,即使存在值,noteData.title
也不会读取任何值。
我收到此错误
Cannot read property 'title' of undefined
I've checked this question但我的代码不包括两次角度,如果是,我该如何检查?
$scope.addNewNote=function(noteData){
alert(noteData.title);
}
index.html链接
<!-- Ionic javascript lib -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- end Ionic javascript lib -->
<!-- Angular javascript lib -->
<script src="lib/angular-messages.js"></script>
<script src="lib/angular-aria.js"></script>
<script src="lib/angular-material.js"></script>
<!-- end Angular javascript lib -->
<!-- Cordova script (this will be a 404 during development) -->
<script src="lib/ng-cordova.js"></script>
<script src="cordova.js"></script>
更新 app.js文件
.state('app.addnote', {
url: "/addnote",
cache: false,
views: {
'menuContent': {
templateUrl: "templates/note/html/note-add.html",
controller: 'AddNotesCtrl'
}
}
})
.state('app.notelist', {
url: "/notelist",
params:{
isAnimated:true
},
cache: false,
views: {
'menuContent': {
templateUrl: "templates/application-storage/local-application-db/html/note-list.html",
controller: 'noteListCtrl'
}
}
}
)
Controller.js
var appControllers = angular.module('starter.controllers', []); // Use for all controller of application.
var appServices = angular.module('starter.services', []);// Use for all service of application.
appControllers.controller('AddNotesCtrl',function($scope,$rootScope,$state,$mdToast){
}
答案 0 :(得分:1)
我不知道为什么它会两次触发/提交(你确定没有双重功能声明或者在html和路由中声明控制器两次等等吗?)。它不会对我这样做,但对于第二个问题,它不提交数据。您是否在控制器中的任何位置声明了变量noteData
?如果你在控制器中设置它,然后像这样捕获提交函数,那么它将显示数据:
$scope.noteData = [];
$scope.addNewNote = function(noteData) {
console.log('form submitted');
console.log(noteData);
};
修改强>
请在SO上查看this post,了解表单提交两次的问题。您的问题不会与您在问题中提供的代码重复,因此您必须在某处导致此问题。