4

我有一个required tag在我的输入中使用的表单 - 这工作正常,但在我的提交按钮上,我有在ng-click="visible = false"提交数据时隐藏表单。

如果所有验证都正确,我怎么能使它只设置为 false?

应用程序.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>
4

1 回答 1

4

如果您为表单命名,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;
        }
    }
}

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

于 2013-03-16T22:57:08.620 回答