6

我使用 angular-material 构建我的应用程序登录表单,并尝试使用它与他们页面上的示例有所不同。我只希望必填字段仅在用户尝试提交而不填写字段时对我大喊大叫。也许也禁用按钮?无论如何,如果用户只填写一个字段并按下回车键,我希望它显示“这是必需的”。现在它只是一直显示它,直到你填写它

这是我所拥有的:https ://jsfiddle.net/5b1tsm2a/6/

我正在尝试他们的示例页面中的代码,如下所示 -

  <form ng-submit="authenticate()" name="loginForm">
        <md-input-container>
            <label>Username</label>
            <input name="name" reauired ng-model="loginInfo.username">
            <div ng-messages="projectForm.description.$error">
                <div ng-message="required">This is required.</div>
            </div>
        </md-input-container>

        <md-input-container>
            <label>Password</label>
            <input name="password" reauired type="password" ng-model="loginInfo.password">
            <div ng-messages="projectForm.description.$error">
                <div ng-message="required">This is required.</div>
            </div>
        </md-input-container>

        <md-button ng-click="authenticate()" class="md-primary btn btn-primary">Login</md-button>
    </form>

所以我正在使用所需的逻辑和它下面的 ng-messages。我只想显示他们是否尝试在不填写任何内容的情况下提交。谢谢!

4

1 回答 1

5

更新您的代码如下:

<form ng-submit="authenticate()" name="loginForm">
        <md-input-container>
            <label>Username</label>
            <input name="username" ng-model="loginInfo.username" required/>
            <div ng-messages="loginForm.username.$error" ng-if='loginForm.username.$dirty'>
                <div ng-message="required">This is required.</div>
            </div>
        </md-input-container>

        <md-input-container>
            <label>Password</label>
            <input name="password" type="password" ng-model="loginInfo.password" required/>
            <div ng-messages="loginForm.password.$error" ng-if='loginForm.password.$dirty'>
                <div ng-message="required">This is required.</div>
            </div>
        </md-input-container>
        <md-button type="submit" class="md-primary btn btn-primary">Login</md-button>
 </form>
于 2015-03-31T15:32:09.287 回答