当我尝试调用ng-submit - submitForm()函数时,我无法调用该函数,它也没有做任何事情。我该如何解决?
(function () {
'use strict';
/**
* @ngdoc function
* @name app.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the app
*/
angular.module('app')
.controller('MainCtrl', ['$scope', 'cordova', function ($scope, cordova) {
console.log("Hello...");
cordova.ready.then(function () {
alert('Cordova is ready');
console.log("Ready....");
});
// function to submit the form after all validation has occurred
this.submitForm = function() {
console.log("Submit form...");
// check to make sure the form is completely valid
if ($scope.userForm.$valid) {
alert('our form is amazing');
console.log("For submitted..")
}
};
}]);
})();

答案 0 :(得分:0)
我无法确定您是否正在使用'控制器作为'语法,但除此之外,您必须将submitForm函数添加到范围:
$ scope.submitForm = function ...
答案 1 :(得分:0)
注意:我会假设cordova.ready
正在运行,但只有当您的应用在模拟器或物理设备中运行时才能解决。
我建议在this.submitForm
方法中检查cordova是否准备就绪。
(function () {
'use strict';
angular.module('app')
.controller('MainCtrl', ['$scope', 'cordova', function ($scope, cordova) {
console.log("Hello...");
// function to submit the form after all validation has occurred
this.submitForm = function() {
console.log("Submit form...");
cordova.ready.then(function () {
alert('Cordova is ready');
console.log("Ready....");
validateAndSendForm();
}, function() {
console.log('It is not cordova, decide to send or not the form.');
// validateAndSendForm();
});
};
function validateAndSendForm() {
// check to make sure the form is completely valid
if ($scope.userForm.$valid) {
alert('our form is amazing');
console.log("For submitted..")
}
}
}]);
})();
您的HTML应该类似于:
<div ng-controller="MainCtrl as main">
<form method="post" ng-submit="main.submitForm()">
<!-- More fields here -->
<button type="submit">Submit</button>
</form>
</div>