【问题标题】:Close ng-bootstrap modal关闭 ng-bootstrap 模式
【发布时间】:2017-08-21 08:46:52
【问题描述】:

我在我的 angular 4 项目中使用 ng-bootstrap 并且在项目的一部分中我使用了包含表单的模式,当我提交表单时,我希望 modal 被关闭.本节我有两个组件:

1:点击运行模态
2:模态框内的表单验证

我能做什么?

我的代码是:

<ng-template #signin let-c="close" let-d="dismiss">
    <div class="modal-header">
        <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <div class="row">
            <div class="col-12 mt-5 mn-15px mb-2">
                <div class="wrapper-left-content">
                    <app-login-form></app-login-form>
                </div>
            </div>
        </div>
    </div>
</ng-template>

login.component.html

<form [formGroup]="loginForm" (ngSubmit)="login(loginForm.value)" class="form-horizontal">
    <div class="form-group" [ngClass]="{ 'has-error': !(Username.valid || Username.untouched)}">
        <input class="form-control" type="text" [formControl]="Username" placeholder='sample'>
        <div [hidden]="Username.valid || Username.untouched">
            <span class="text-danger" [hidden]="!Username.hasError('required')">
                'sample'
            </span>
        </div>
    </div>
    <div class="form-group" [ngClass]="{ 'has-error': !(Password.valid || Password.untouched)}">
        <input class="form-control" type="Password" [formControl]="Password" placeholder='sample'>
        <div [hidden]="Password.valid || Password.untouched">
            <span class="text-danger" [hidden]="!Password.hasError('required')">
                    'sample' !
            </span>
            <span class="text-danger" [hidden]="!Password.hasError('minLength')">
        'sample' !
            </span>

        </div>
        <a class="fs-12 pull-left my-2" href="#">'sample'؟</a>
    </div>

    <div class="form-group">
        <button type="submit" [disabled]="!loginForm.valid"  class="btn btn-md btn-info btn-block">'sample'</button>
    </div>
</form>

login.component.ts

export class LoginComponent {
  @Output() cancel = new EventEmitter();
  errorMessage: string;

  Username = new FormControl('', [
    Validators.required,
  ]);
  Password = new FormControl('', [
    Validators.required,
    Validators.minLength(6)
  ]);
  loginForm: FormGroup = this.builder.group({
    Username: this.Username,
    Password: this.Password
  });

  constructor(private builder: FormBuilder, private auth: AuthService, private router: Router,
              private toastr: ToastsManager, vcr: ViewContainerRef , public modalService: NgbModal) {
    this.toastr.setRootViewContainerRef(vcr);


  }


  login(values: any) {
    this.auth.login(values)
      .subscribe(
        loggedIn => {
          if (loggedIn) {
            this.router.navigateByUrl('');
            this.toastr.success('sample', null, {toastLife: 4000});
            this.auth.login_again(values)
              .subscribe(
                () => console.log(' login_again is working !')
              );
          }
        },

      );
  }

【问题讨论】:

  • 请提供更多信息,最重要的是更多/您的代码。

标签: angular modal-dialog ng-bootstrap


【解决方案1】:

您必须向您的登录组件添加一个新输出,并在用户登录时发出此输出:

export class LoginComponent {
    @Output() closeModal: new EventEmitter<any>();
...
login(values: any) {
    // add it here when you want the modal closed in every case (also when
    // the login fails)
    this.closeModal.emit();

    this.auth.login(values)
        .subscribe(
    loggedIn => {
      if (loggedIn) {
        this.router.navigateByUrl('');

        // add the "this.closeModal.emit()" here if you want the modal 
        // closed when the user is logged in

        this.toastr.success('sample', null, {toastLife: 4000});
        this.auth.login_again(values)
          .subscribe(
            () => console.log(' login_again is working !')
          );
      }
    },

  );
}

此外,您必须在模态中收听这个新事件:

<ng-template #signin let-c="close" let-d="dismiss">
<div class="modal-header">
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
<div class="modal-body">
    <div class="row">
        <div class="col-12 mt-5 mn-15px mb-2">
            <div class="wrapper-left-content">
                <!-- you can use the assigned variable "c" or "d" from the  
                     ng-template-->
                <app-login-form (closeModal)="c()"></app-login-form>
            </div>
        </div>
    </div>
</div>

变量c(来自let-c="close")和d(来自let-d="dismiss")是从ngb-modal传入模板的方法。例如,您可以在模态标题中看到方法“d”的调用。 更多信息见:https://ng-bootstrap.github.io/#/components/modal/examples

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多