Мне нужно очистить предметы (localstorage
) от корзины, когда пользователь проверяет.
Я просто хочу запустить это, а затем отправить форму.
// clear the cart
shoppingCart.prototype.clearItems = function () {
this.items = [];
this.saveItems();
}
однако ng-click или ng-submit dont fire, он просто отправляет. форма
<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>
Не мог найти ответ, который я искал, прошу пролить свет.
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
Попробуй это,
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*/});
};
Удалите action
из формы и ng-click
из кнопки отправки. Таким образом, перенаправление на страницу проверки в submitCart()
Прежде всего, я хотел бы сказать, что заголовок вопроса вводит в заблуждение. Вам не нужны как ng-submit
и ng-click
для той же операции.
Используйте эту услугу для отправки данных с угловых символов туда, куда вы хотите отправить их. 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.
});
}
Напротив, я предлагаю вам очистить поле ввода после работы с обратным вызовом с сервера.