stackov8
stackov8

Reputation: 434

how to cancel a form of controller?

help please cancel sending the form. after pressing the submit button to be displayed in the console message:

stop submitForm! false

and sending the form should be blocked. because

$scope.message = false

js:

var briefApp = angular.module("briefApp", []);

briefApp.controller("briefController", function ($scope) {
    $scope.attachmentFilesize = false;
    $scope.attachmentFileformat = false;
    $scope.name = false;
    $scope.email = false;
    $scope.phone = false;
    $scope.message = false;

    $scope.submitForm = function (event) {
        if($scope.message == false){
            console.log('stop submitForm! false');
        }else{
            console.log('submitForm! true');
        }
    }    
})

html:

<form enctype="multipart/form-data" name="brief_form_new" id="brief_form_new_test" action="#" method="post" novalidate="novalidate" ng-app="briefApp" ng-controller="briefController" ng-submit="submitForm()" >
    <textarea   type="text" 
                name="message_new" 
                rows="5" 
                cols="50" 
                class="main_field"
                placeholder="Я хочу... *" 
                style="width: 426px;" 
                title="Заполните поле"></textarea>

    <input type="submit" value="Отправить" class="brief_submit_new" />
</form> 

fillde

Upvotes: 0

Views: 73

Answers (1)

A.B
A.B

Reputation: 20445

You need to pass $event from the view into the function and then stop defualt event occurrence by preventDefault()

 ng-submit="submitForm($event)"

Controller

$scope.submitForm = function ($event) {
        if($scope.message == false){
            console.log('stop submitForm! false');
        }else{
            console.log('submitForm! true');
        }
       $event.preventDefault();
    }    

Upvotes: 1

Related Questions