Angular JS HTML5验证

前端之家收集整理的这篇文章主要介绍了Angular JS HTML5验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表单,我已经在我的输入上使用了所需的标签 – 这工作正常但在我的提交按钮上我有ng-click =“visible = false”,在提交数据时隐藏表单.

如果所有验证都正确的话,我怎么能把它设置为假?

App.js

$scope.newBirthday = function(){

        $scope.bdays.push({name:$scope.bdayname,date:$scope.bdaydate});

        $scope.bdayname = '';
        $scope.bdaydate = '';

    };

HTML:

<form name="birthdayAdd" ng-show="visible" ng-submit="newBirthday()">
        <label>Name:</label>
        <input type="text" ng-model="bdayname" required/>
        <label>Date:</label>
        <input type="date" ng-model="bdaydate" required/>
        <br/>
        <button class="btn" ng-click="visible = false" type="submit">Save</button>
      </form>

解决方法

如果您为FormController提供一个名称,FormController将通过其名称在最近的控制器范围中公开.所以在你的情况下说你有一个类似的结构

<div ng-controller="MyController">
    <!-- more stuff here perhaps -->
    <form name="birthdayAdd" ng-show="visible" ng-submit="newBirthday()">
        <label>Name:</label>
        <input type="text" ng-model="bdayname" required/>
        <label>Date:</label>
        <input type="date" ng-model="bdaydate" required/>
        <br/>
        <button class="btn" ng-click="onSave()" type="submit">Save</button>
      </form>
</div>

现在在你的控制器中

function MyController($scope){
    // the form controller is now accessible as $scope.birthdayAdd

    $scope.onSave = function(){
        if($scope.birthdayAdd.$valid){
            $scope.visible = false;
        }
    }
}

如果表单内的输入元素有效,则表单有效.希望这可以帮助.

猜你在找的Angularjs相关文章