2016-07-26 29 views
0

當用戶檢出時,我需要從購物車清除項目(localstorage)。未啓用ng提交

我只想運行這個,然後提交表單。

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

但是,ng-click或ng-submit不會觸發,它只是提交。 形式

<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

從提交按鈕中刪除actionng-click。所以重定向到結帳頁面的submitCart()方法

0

試試這個範圍內,

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

所有我想首先說,這個問題的標題具有誤導性。對於相同的操作,您不需要同時使用ng-submitng-click

使用此服務可以將數據從angularjs發送到要發送到的任何位置。 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