2016-07-26 7 views
0

ユーザーがチェックアウトするときに、カートから項目(localstorage)をクリアする必要があります。提出時に提出を提出しない

これを実行してフォームを送信したいだけです。

// clear the cart 
shoppingCart.prototype.clearItems = function() { 
    this.items = []; 
    this.saveItems(); 
} 

ただし、クリックしないと送信できません。送信するだけです。

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

は私が探していた答えを見つけることができませんでした フォームは、いくつかの光を当てるしてください。

+0

submitCart()関数のコードを共有できますか? –

+0

あなたはjsfiddleリンクを提供できますか? –

+0

私は角度のあるフォームを提出する "イベント"をキャッチしてから、通常のHTMLフォームを投稿したいと考えていました。 –

答えて

0

フォームからactionを削除し、送信ボタンからng-clickを削除します。だから、submitCart()方法

0

内のチェックアウトページへのリダイレクトは、

<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

私は質問者のことを言いたいまず

HTMLを、これを試してみてくださいタイトルは誤解を招く。あなたは同じ操作のためにng-submitng-clickの両方が必要ないでしょう。

このサービスを使用して、anglejsから送信先にデータを送信します。逆に https://docs.angularjs.org/api/ng/service/ $ HTTP

フォーム

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

コントローラ

$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. 
    }); 
} 

私は、サーバーからのコールバックでの作業の後に入力フィールドをクリアするためにあなたをお勧めしたいです。

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