2016-07-26 8 views
0

사용자가 체크 아웃 할 때 카트에서 항목 (localstorage)을 삭제해야합니다.제출시 제출 안함 제출

난 그냥 실행하고 양식을 제출 싶습니다.

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

양식에서 action을 제거하고 제출 버튼에서 ng-click을 제거하십시오. 그래서

<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

이을 시도해보십시오 submitCart() 메서드 내에서 체크 아웃 페이지로 리디렉션 그 질문의 제목이 오해의 소지가 있습니다. 동일한 작업을 수행하려면 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

먼저 말을

HTML,

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 
관련 문제