未调用提交表单和 cordova 函数

Submit form and cordova functions are not getting called

当我尝试调用 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..")
        }
      };
  }]); 
  
})();

我真的看不出你是否在使用 'Controller as' 语法,否则,你必须将 submitForm 函数添加到范围中:

$scope.submitForm = 函数...

注意:我会假定 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>