2016-04-18 3 views
0

Wenn ich versuche, die ng-submit - submitForm() -Funktion aufzurufen, kann ich die Funktion nicht aufrufen, und es tut nichts. Wie löse ich es?Senden Formular und Cordova Funktionen werden nicht aufgerufen

(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..") 
 
     } 
 
     }; 
 
    }]); 
 
    
 
})();

Antwort

0

Hinweis: Ich nehme an, dass die cordova.ready funktioniert, aber es wird nur gelöst, wenn Ihre App entweder in einem Emulator oder in einem physischen Gerät ausgeführt wird.

Ich schlage vor zu überprüfen, ob Cordova innerhalb der this.submitForm Methode bereit ist.

(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..") 
     } 
     } 
    }]); 

})(); 

Ihre html sollte in etwa so aussieht:

<div ng-controller="MainCtrl as main"> 
    <form method="post" ng-submit="main.submitForm()"> 
    <!-- More fields here --> 
    <button type="submit">Submit</button> 
    </form> 
</div> 
+0

Ich habe es in app.js as - (function() { 'Verwendung streng'; Winkel .module ('app' [ 'ngRoute', 'ngCookies']) CONFIG (config); . Config $ inject = [ '$ routeProvider', '$ locationProvider']; Funktion config ($ routeProvider, $ locationProvider) { $ routeProvider .when ('/', { Controller: 'MainCtrl', templateUrl: 'views/main.html', controllerAs: 'Haupt' }) .otherwise ({redirectTo: '/'}); } })(); – Smitha

+0

Großartig @Smitha, dann das: 'ng-controller =" MainCtrl as main "' ist nicht notwendig. – slackmart

0

Ich kann nicht wirklich sehen, ob Sie die 'Controller als' Syntax verwenden, aber ansonsten haben Sie die submitForm Funktion auf den Umfang hinzuzufügen:

$ scope.submitForm = function ...

+0

Ich habe es in app.js als - (function() { 'use strict'; Winkel .module ('App', [ 'ngRoute ',' ngCookies ')) .config (config); config. $ inject = [' $ routeProvider ',' $ locationProvider ']; Funktion config ($ routeProvider, $ locationProvider) {$ routeProvider .when ('/', { controller: 'MainCtrl', templateUrl: 'views/main.html', controllerAs: 'main'}) .otherwise ({redirectTo: '/'});}})(); – Smitha

Verwandte Themen