2016-07-26 13 views
0

Ich muss Artikel (localstorage) aus dem Warenkorb löschen, wenn Benutzer auscheckt.Nicht feuern ng-submit on submit

Ich möchte nur dies ausführen und dann das Formular absenden.

jedoch ng-klicken oder ng-submit nicht feuern, es reicht nur. die Form

<form novalidate action="http://localhost:8000/checkout" method="POST" 
ng-submit="submitCart()" class="form"> 
    <input class="btn btn-primary btn-flat" type="submit" ng-click="submitCart()" name="Checkout"/> 
</form> 

Könnte die Antwort nicht gefunden ich suchte, Schuppen bitte etwas Licht.

+0

Können Sie den Code der Funktion submitCart() teilen? –

+0

könnten Sie eine jfiddle-Verbindung bereitstellen? –

+0

Ich hatte gehofft, das "Ereignis" der Einreichung eines Formulars mit eckigen zu fangen, und dann fortfahren, ein normales HTML-Formular zu veröffentlichen. –

Antwort

0

Entfernen Sie die action aus dem Formular und ng-click von der Senden-Schaltfläche. So umleiten die zur Kasse Seite innerhalb der submitCart() Methode

0

Try this,

html

<form novalidate action="#" 
    ng-submit="submitCart()" class="form"> 
     <input class="btn btn-primary btn-flat" type="submit" ng-click="submitCart()" name="Checkout"/> 
    </form> 

js

$scope.submitCart= function() { 
    var data = {} 

      $http.post('http://localhost:8000/checkout', JSON.stringify(data)).success(function(){/*success callback*/}); 
     }; 
0

allererst Ich mag würde sagen, dass die Frage des Titel ist irreführend. Sie benötigen nicht beide ng-submit und ng-click für den gleichen Vorgang.

Verwenden Sie diesen Dienst, um Daten von angularjs an die Stelle zu senden, an die Sie sie senden möchten. https://docs.angularjs.org/api/ng/service/ $ http

Ihr Formular

<form method="POST" class="form"> 
    <input class="btn btn-primary btn-flat" type="submit" ng-click="submitCart()" name="Checkout"/> 
</form> 

-Controller

$scope.submitCart = var function() 
{ 
//Call the function to clear the items here. 
//And now http call to backend. 
$http({ 
    method: 'POST', 
    url: '/someUrl' 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 

    }, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 
} 

Im Gegenteil würde ich Sie vorschlagen, um das Eingabefeld zu löschen, nachdem sie mit dem Rückruf vom Server arbeiten.

0
You need to remove the action="http://localhost:8000/checkout " from the above html for the code to work and you can use a $http service in controller to post data to that url 

the correct html is :- 

<form novalidate 
ng-submit="submitCart()" class="form"> 
    <input class="btn btn-primary btn-flat" type="submit" ng-click="submitCart()" name="Checkout"/> 
</form> 

and in controller i.e app.js:- 
app.controller("abc",function($scope,$http){ 
$scope.user={}; 
$http.post("http://localhost:8000/checkout",{data}) 
.success(function(data){ // response data back from server 

}) 
}) 

and bind the input values in form with ng-model on any object and initialise that object in controller like <input type="text" ng-model='user.name' /> 

and when sending data replace data in $http.post with $scope.user and you can get the data in req.body on server side