如何取消控制器的形式?

时间:2015-03-01 18:09:37

标签: angularjs

帮助请取消发送表格。按下提交按钮后显示在控制台消息中:

  

停止submitForm!假

并且应该阻止发送表单。因为

$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

1 个答案:

答案 0 :(得分:1)

您需要将$event从视图传递到函数中,然后通过preventDefault()

停止defualt事件的发生
 ng-submit="submitForm($event)"

控制器

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