2016-05-14 58 views
1

的提交的錯誤,我想顯示所需的錯誤消息,消息一旦從被提交,但不知道該怎麼做我試圖如下圖所示如何在從角JS

<div class="controls"> 
     <form name="formx" > 
     <ul class="form small"> 
       <div class="tag">ID</div> 
       <input type="text" class="small" name="enrolmentId" ng-model="enrolmentDetail.Id" required="" onkeypress='return !(event.charCode == 32)'> 
       <div class="error text-danger" style="position: absolute;margin-left: 120px;" ng-show="formx.enrolmentId.$error.required && (formx.$submitted || formx.enrolmentId.$touched)"><p>Id is required</p></div> 
      </li> 
      <li> 
       <div class="btn" ng-click="saveDetails(enrolmentDetail)" ng-show="formAction=='save'">SAVE</div> 
      </li> 
     </ul> 
      </form> 
    </div> 
現在

這裏錯誤消息顯示一旦我們骯髒的文本框,但我想顯示錯誤消息一旦表單提交,如果有需要的錯誤不應該提交

嘗試使用ng-submit但不知道該怎麼做

請幫助創建一個小提琴或postin g的例子做

回答

1

這是一個example

HTML

<body ng-app="myApp"> 
<div ng-controller="myCtrl as mc"> 
<form class="form" name="myForm" ng-submit="mc.submit(myForm)" novalidate> 
    <div class="form-group"> 
    <label for="username">Username</label> 
    <input name="username" class="form-control" type="text" ng-model="mc.username" required/> 
    <p class="text-danger" ng-show="myForm.username.$error.required && mc.submitted">Username is required</p> 
    </div> 
    <div class="form-group"> 
    <label for="password">Password</label> 
    <input name="password" class="form-control" type="password" ng-model="mc.password" required/> 
    <p class="text-danger" ng-show="myForm.password.$error.required && mc.submitted">Password is required</p> 
    </div> 
    <button class="btn btn-success">submit</button> 
</form> 
<p class="text-success" ng-show="mc.sent && mc.submitted">Form sent</p> 

JS

angular.module("myApp",[]) 
    .controller("myCtrl", function(){ 
    var vm = this; 
    vm.submit = submit; 
    function submit(form){ 
     vm.submitted = true; 
     if(form.$valid && vm.submitted === true){ 
     //Send data logic 
     vm.sent = true; 
     } 
    } 
+1

謝謝你,我RVIN!這工作像魔術一樣,很好,很簡單!希望我可以將此答案標記爲已接受! – Nat