表单验证在 IONIC 上不起作用,但在 AngularJS 上起作用

Form Validation Not Working on IONIC but working on AngularJS

我必须在我的离子应用程序中提供必填字段验证。但是,我无法使默认的 angular 表单验证正常工作。我使用了 ng-submit 但在 ionic 中甚至没有触发提交。 有没有我可以使用的指令

<ion-view title="Dashboard">
  <ion-content class="has-header padding">
    <form name="myForm" ng-controller="DashCtrl">
      <input type="radio" ng-model="color" value="red" required>  Red <br/>
      <input type="radio" ng-model="color" value="green" required> Green <br/>
      <input type="radio" ng-model="color" value="blue" required> Blue <br/>
      <tt>color = {{color | json}}</tt><br/>
      <button type="submit" ng-click="submitForm()">Submit</button>
    </form>
  </ion-content>
</ion-view>

但是,如果它是一个 AngularJS 应用程序,它会按预期工作

AngularJS 网友:https://plnkr.co/edit/EdItU2IIkO1KIsC052Xx?p=preview

IONIC 插件:https://plnkr.co/edit/tr7btg7mr0SHhbOB?p=preview

对于 HTML5 在 Ionic 中不工作,检查可能 this answer from html5 validation in Ionic

但是,您也可以这样做:

将您的提交更改为 ng-click="submitForm(myForm)"

你的提交功能是这样的

   $scope.submitForm = function(form) {
     if (form.$valid == true){
       console.log('yay')
     } else {
       console.log('Nyay')
     }
    };

通过此更改分叉您的 plnkr,检查它 HERE

希望对您有所帮助!