【问题标题】:Modal close and form value reset not working in Angular模态关闭和表单值重置在 Angular 中不起作用
【发布时间】:2019-02-03 19:49:52
【问题描述】:

与许多与我的问题相关的现有问题交叉,我发布了这个。所以不要将此评论为重复并发送重复的答案,我的情况有点不同。所有解决方案都与 javascript 或 jquery 有关。但我想要 Angular。

我正在使用 mdBootstrap,只提供 index.html 中的 CDN 链接,而不是安装它。我有一个带有登录和注册选项卡的弹出窗口。问题是在输入凭据并单击登录模式后没有关闭(我不想放置 data-dismiss="modal"),因为它在不执行登录功能的情况下关闭了模式。我希望完成这两个功能,验证凭据是否无效,我希望模式处于打开状态,说错误消息。如果有效则只关闭模态。

第二件事我想重置模态表单值,即使我单击模态之外的任何位置并重新打开模态也是如此。例如,如果我有两个输入字段,如用户名和密码,我只输入了用户名,将密码留空,然后单击外部并重新打开模式,我的模式使用输入的用户名值打开。我想摆脱这些问题。

任何人都可以帮助我解决这些问题。提前致谢。

Log-Reg.component.html

<div class="modal fade" id="modalLoginRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        //tabs for login & Register
        <div class="tab-content py-0">
          <div class="tab-pane fade in show active" id="modalLoginTab" role="tabpanel">
            <form #loginForm="ngForm">
              <div class="md-form">
                <input required [(ngModel)]="username" type="email" id="loginEmail" name="Username" class="form-control validate">
                <label for="loginEmail">Enter your email address</label>
              </div>
              <div class="md-form">
                <input required [(ngModel)]="password" type="password" id="loginPassword" name="Password" class="form-control validate">
                <label for="loginPassword">Enter your password</label>
              </div>
              <div *ngIf="isLoginError" class="alert alert-danger">Incorrect Credentials</div>
              <button type="button" [disabled]=!loginForm.valid class="btn btn-primary" (click)="onLogin()">Login</button>
            </form>
          </div>
          //Form for Register
        </div>
      </div>
    </div>
  </div>
</div>

Log-Reg.component.ts

@ViewChild('loginForm') public userLoginForm : NgForm;

onLogin(){
//some function for login validation\
this.userLoginForm.reset();
}

【问题讨论】:

  • 在 stackblitz 中添加你的代码
  • 实际上模态没有关闭或验证没有完成?我无法告诉你真正的问题是什么
  • 验证完成,但模式没有关闭。只有当我点击顶部的关闭按钮时,模式才会关闭
  • @Arthi 无法访问代码编辑器

标签: angular mdbootstrap


【解决方案1】:

根据您的评论,我假设您在登录后无法关闭模式,

您没有在组件中导入模态指令

模态模板 .html 文件

 <div mdbModal #loginModal="mdbModal" class="modal fade" id="modalLoginRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
        //tabs for login & Register
        <div class="tab-content py-0">
          <div class="tab-pane fade in show active" id="modalLoginTab" role="tabpanel">
            <form #loginForm="ngForm">
              <div class="md-form">
                <input required [(ngModel)]="username" type="email" id="loginEmail" name="Username" class="form-control validate">
                <label for="loginEmail">Enter your email address</label>
              </div>
              <div class="md-form">
                <input required [(ngModel)]="password" type="password" id="loginPassword" name="Password" class="form-control validate">
                <label for="loginPassword">Enter your password</label>
              </div>
              <div *ngIf="isLoginError" class="alert alert-danger">Incorrect Credentials</div>
              <button type="button" [disabled]=!loginForm.valid class="btn btn-primary" (click)="onLogin()">Login</button>
            </form>
          </div>
          //Form for Register
        </div>
      </div>
    </div>
  </div>
</div>

在您的组件文件 (appcomponent.ts) 中试试这个

import { ModalDirective } from 'ng-mdb-pro/free/modals/modal.directive';

    @ViewChild('loginModal') _loginModal: ModalDirective;

    onLogin(){
    *add your login logic in your success add the below code*
    this._loginModal.hide()
    }

希望这能解决你的问题

【讨论】:

  • 我在问题中提到我没有安装 mdBootstrap 只是使用 CDN 链接。
  • 在不安装 mdBootstrap 的情况下有没有其他方法可以解决这个问题?
  • 我不这么认为,在 CDN 中您无法访问他们的 ModalDirective,如果您仍然需要它,请创建您自己的指令,该指令可以访问您可以控制的元素
  • 嗨@Arthi 请检查angular-qxc8rh.stackblitz.io 并告诉我这个网址工作正常吗?
  • 嗨@KrishnaRathore,你能提供stackblitz的开发者代码链接吗?
【解决方案2】:

试试这个。

this.userLoginForm.form.reset()

this.userLoginForm.resetForm()

【讨论】:

  • this.userLoginForm 应该是 FormGroup。如果是,则会出现错误form doesn't exist on FormGroup
  • @KrishnaRathore 如何在此处进行验证。表单值无需验证即可重置
  • 好的,让我检查一下
  • 我不知道如何硬编码登录值。我正在为此使用 web api 服务,所以无法发布它
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2017-01-16
  • 2022-08-05
  • 2021-06-23
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多