提交表单和cordova函数没有被调用

时间:2016-04-18 07:09:43

标签: javascript angularjs cordova angular-controller

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




2 个答案:

答案 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>