2017-04-03 3 views
1

각도 상자의 양식 필드를 확인하려고했지만 버튼을 클릭하면 오류 메시지가 표시됩니다. 나는 내 코드에서 문제가 무엇인지 알 수 없다.각도 js의 양식 유효성 검사 시도

var myApp = angular.module('myApp', ['ngMessages']); 
myApp.controller("myController",function($scope){ 
    $scope.name=undefined; 
    $scope.showMsgs = false; 
    $scope.send = function(){ 
     if ($scope[form].$valid) { 
      $scope.showMsgs = false; 
     } else { 
      $scope.showMsgs = true; 
     } 

    } 
}); 

: 내가 버튼을 클릭했을 때 오류 메시지가 표시되지만 문제는 페이지로드에서의 쇼 오류 메시지입니다

내 코드

HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Layout</title> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-messages/1.6.4/angular-messages.min.js"></script> 
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet"> 
    <link rel="stylesheet" href="css/style.css"> 
    <link rel="stylesheet" href="css/mediaStyle.css"> 

<script type="text/javascript"> 
    $(document).ready(function() { 
    $(document).on("click", ".square-radio", function() { 
     $(this).toggleClass("square-radio--clicked"); 
    }); 
}); 
</script> 
</head> 
<body ng-app="myApp"> 

<div class="container"> 
    <header id="logoHeader"> 
     <h1>Logo</h1><span class="fa fa-times" aria-hidden="true"></span> 
    </header> 
    <div class="emailSupportPanel"> 
     <div class="mainContainer"> 
      <h1 class="panel-contentHeader">UK & Ireland Email Support</h1> 
      <p>Step1:Tell us your issue > Step2: Tell us your issue<span> > Step3:Tell us your issue</span></p> 
     </div> 
    </div> 
    <div class="mainContainer"> 
     <h4 class="imaportantNote">Feilds marked with an astrick <span class="fa fa-asterisk" aria-hidden="true star-icon"></span> are required</h4> 
     <form name ="myform" ng-submit="sumsung-form.$valid" novalidate ng-init="disabled=false"> 
     <div class="form-container mb15" ng-class="{true: 'error'}[submitted && sumsung-form.userSubject.$invalid]"> 
      <label class="form-label">Subject <span class="fa fa-asterisk" aria-hidden="true star-icon"></span></label> 
      <input type="text" class="form-input" name="userSubject" 
        ng-model="userSubject" id="subject" ng-required="!disabled" ng-disabled="disabled" 
        ng-required="true" 
        placeholder="Enter Subject" ng-class="{error: showMsgs && myform.name.$error.required}"> 
      <p ng-show="showMsgs && myform.name.$error.required">This field is required</p> 
      <!-- <div ng-messages="sumsung-form.userSubject.$error" 
      ng-show="sumsung-form.userSubject.$touched || sumsung-form.$submitted"> 
       <div ng-message="required" class="state-error"><i class="fa fa-exclamation-circle" aria-hidden="true" ng-show="submitted"></i>This field is required</div> 
      </div>--> 
     </div> 
     <div class="form-container"> 
      <label class="form-label" ng-class="{ 'state-error': sumsung-form.message.$invalid && (sumsung-form.message.$touched || sumsung-form.$submitted)}">Message <span class="fa fa-asterisk" aria-hidden="true star-icon"></span></label> 
      <textarea class="form-input" name="userMessage" ng-model="message" rows="5" 
         id="message" style="border-radius:0;" 
         ng-required="true" 
         placeholder="Enter Message"></textarea> 
     <!-- <div ng-messages="sumsung-form.userMessage.$error" 
       ng-show="sumsung-form.userMessage.$touched || sumsung-form.$submitted"> 
       <div ng-message="required" class="state-error"><i class="fa fa-exclamation-circle" aria-hidden="true"></i>This field is required</div> 
       <div ng-message="minlength" class="state-error">Message must be over 100 characters</div> 
       <div ng-message="maxlength" class="state-error">Message must not exceed 1000 characters</div> 
      </div>--> 
      <!-- <p class="error-message">Please enter more then 50 charcters.</p>--> 
     </div> 

     <div class="footer"> 
      <button type="submit" ng-click="send('myform')" class="btn">SEND</button> 
     </div> 
     </form> 
</div> 

</body> 
</html> 

JS입니다 CSS :

body{ 
     font-size:12px; 
     color:#333; 
     font-family: 'Raleway', sans-serif; 
     margin:0; 
     padding: 0; 
    } 
    .mb15{ 
     margin-bottom: 15px; 
    } 
    .container{ 
     position: relative; 
     width: 100%; 

    } 

    #logoHeader{ 
     width: 100%; 
     height:70px; 
     line-height: 70px; 
     max-height: 70px; 
     border-bottom: 1px solid #eee; 
     text-align: center; 

    } 
    #logoHeader>h1{ 
     margin: 0 !important; 
     color: #1428A0; 
    } 
    #logoHeader>span{ 
     display: inline; 
     position: relative; 
     right: -45%; 
     top: -60px; 
     font-size: x-large; 
     color: #ccc; 
    } 
    .emailSupportPanel{ 
     position: relative; 
     width: 100%; 
     height: 80px; 
     padding: 12px 0; 
     background: rgba(238, 238, 238, 0.5); 
     box-sizing: border-box; 
    } 
    .panel-contentHeader{ 
     margin: 0; 
     font-weight: bold; 
    } 
    .panel-contentHeader ~p>span{ 
     color:#1428A0; 
    } 
    .mainContainer{ 
     position: relative; 
     margin: 0 auto; 
     max-width: 80%; 
    } 
    .mainContainer .imaportantNote{ 
     font-weight: bolder; 
     font-size: 15px; 
     color:#777; 
    } 
    .fa-asterisk:before { 
     color: red; 
     font-size: 8px; 
     vertical-align: super; 
    } 
    .form-container{ 
     width: 100%; 
     height:auto; 
     padding-bottom: 12px; 
    } 
    .form-label{ 
     font-weight: bold; 
     margin-bottom: 10px; 
     font-size: 15px; 
     display: block; 
     color:#000; 
    } 
    .form-input{ 
     display: block; 
     width: 96%; 
     height: 32px; 
     padding: 6px 12px; 
     font-size: 14px; 
     line-height: 1.42857143; 
     color: #555; 
     background-color: #fff; 
     background-image: none; 
     border: 1px solid #ccc; 
     border-radius: 0; 
     -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075); 
     box-shadow: inset 0 1px 1px rgba(0,0,0,.075); 
     -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s; 
     -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s; 
     transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s; 
    } 
    textarea.form-input{ 
     height: auto; 
    } 
    .error-message{ 
     display: none; 
     width: 96.8%; 
     margin-top: 0px; 
     background-color: red; 
     color: #fff; 
     padding: 8px; 
    } 
    .information-form{ 
     width: 100%; 
     padding-top: 15px; 
     position: relative; 
     /* height: calc(100vh - 540px); */ 
     min-height: 344px; 

    } 
    .information-form>h3{ 
     font-weight: bolder; 
    } 
    .left-info,.right-info{ 
     width: 50%; 
     padding-top: 14px; 
     box-sizing: border-box; 
     float: left; 
     padding-right: 33px; 
     position: relative; 

    } 
    .privacy-container{ 
     width: 100%; 
     height:auto; 
     position: relative; 
    } 
    .square-radio { 
     border: 1px solid black; 
     margin: 2px; 
     width: 20px; 
     height: 20px; 
     position: relative; 

    } 
    .square-radio--clicked .square-radio--content{ 
     margin: auto; 
     position: absolute; 
     left: 0; 
     right: 0; 
     top: 0; 
     bottom: 0; 
     background-color: black; 
     width: 16px; 
     height: 16px; 
    } 
    .square-radio--clicked ~.privacy-text,.square-radio ~.privacy-text{ 
     display: inline-block; 
     width: 100%; 
     margin-left: 39px; 
     font-size: 14px; 
     position: relative; 
     top: -21px; 
     color:#000; 
     font-weight: bold; 
    } 
    .privacy-text a{ 
     text-decoration: none; 
     color: #1428A0; 
    } 
    .product-info{ 
     position: relative; 
     width: 100%; 
     padding-top: 20px; 
     box-sizing: border-box; 
    } 
    .confirmation-part{ 
     width:48%; 
     display: inline-block; 
    } 

    .captcha-box{ 
     width: 100px; 
     height: 45px; 
     border: 1px solid #333; 
     display: inline-block; 
     position: relative; 
     top: -39px; 
     left: 20px; 
     margin-right: 19px; 
     text-align: center; 

    } 
    .captcha-box.cross-box{ 
     width: 50px; 
     height: 50px; 
     text-align: center; 
    } 
    .captcha-box .img-cross-icon{ 
     font-size: xx-large; 
     padding: 7px; 
    } 
    .captcha-box h2{ 
     color: blue; 
     margin-top: 11px; 
     letter-spacing: 4px; 
     font-weight: bolder; 
    } 
    .footer{ 
     width:100%; 
     position: relative; 
     min-height: 100px; 
     text-align: center; 
     padding-top: 20px; 
    } 
    .btn{ 
     padding: 12px 38px; 
     border-radius: 45px; 
     background-color: #1428A0; 
     color:#fff; 
    } 
    hr{ 
     border-color: rgba(204, 204, 204, 0.18); 
    } 
    p{ 
     color:#777; 
    } 
    ::-webkit-input-placeholder { /* Chrome/Opera/Safari */ 
     color: #bbb; 
     padding: 5px; 
    } 
    .error { 
     border-color: red; 
     color:red; 
    } 
+0

당신이 단지 관련 코드 블록을 제공하기 위해 편집 할 수 있기를 바랍니다 참조 용 문서에 대한 링크를입니까? – Sajal

+0

css @ sajalS를 무시하십시오 –

답변

1

처음 본인은 ng-ng-click을 모두 사용하여이 문제를 일으키는 동일한 양식이 중복 제출되는 양식 세부 정보를 제출하고 있습니다. 그들 중 하나를 사용하려고하면 잘 작동합니다. 다음은 angularjs 팀의 경고입니다.

경고 : ngClick과 ngSubmit 핸들러를 함께 사용하여 "이중 제출"하지 않도록주의하십시오. ngSubmit이 트리거 될 수있는 자세한 내용은 양식 지시문 설명서를 참조하십시오. 여기

submitting-a-form-and-preventing-the-default-action

이 도움이 :)

+0

이것은 업데이트 된 코드입니다. 코드의 무엇이 잘못되었는지 알려주세요. https://jsfiddle.net/87hjfs9n/1/ @mitrabrihas –

+0

https://jsfiddle.net/87hjfs9n/3/ 이것은 업데이트 된 코드 –

+0

컨트롤러의 폼에 액세스하는 방법에 문제가있었습니다. – mitrabrihas