【问题标题】:How to show a bootstrap modal via an Angular-Service如何通过 Angular-Service 显示引导模式
【发布时间】:2019-10-30 14:25:24
【问题描述】:

我有一个全局错误处理程序,我在其中处理客户端错误和服务器错误。

为了向用户提供反馈,我想打开一个返回错误消息的模式。

因此我实现了一个模态:

import {Component} from '@angular/core';
import {BsModalRef, BsModalService} from 'ngx-bootstrap';
import {Button} from '../../layout-models/button.model';

@Component({
  selector: 'error-modal',
  templateUrl: './error-modal.component.html',
  styleUrls: ['./error-modal.component.scss']
})
export class ErrorModalComponent {
  title: string;
  buttonTitle = 'OK';
  type: 'error';
  button: Button;

  protected modalRef: BsModalRef;

  constructor(protected modalService: BsModalService) {}

  public show(title: string, message: string) {
    this.title = title;
    this.modalRef = this.modalService.show(
      message,
      Object.assign({}, { class: `modal-banner ${this.type}`})
    );
  }

  hide() {
    if (this.modalRef) {
      this.modalRef.hide();
    }
  }
}

在我的通知服务中:

import {Injectable, NgZone} from '@angular/core';
import { ErrorModalComponent } from '../error-modal.component';

@Injectable({
  providedIn: 'root'
})
export class NotificationService {

  public errorModalComponent: ErrorModalComponent;

  showError(title: string, message: string): void {
     this.errorModalComponent.show(title, message);
  }
}

这会导致 Uncaught TypeError: Cannot read property 'show' of undefined

我觉得我犯了一些根本性的错误——这样做的主要目的是建立一个集中的模式。这是可能的还是我需要在我想显示错误处理模式的每个组件中使用 ModalComponent?

【问题讨论】:

  • 您可以在服务中使用 observable 来确定何时显示和隐藏模式。模态将位于 (app.component) 的根部。在app.component.ts 中,您将订阅该可观察对象并根据从订阅中收到的值显示/隐藏模式。
  • 你能举个简单的例子吗?这对我有很大帮助 - 我对订阅不太熟悉,因为我对 Angular 很陌生!
  • 这是一个粗略的想法,您可能需要进行更改。 stackblitz.com/edit/angular-9vnzma

标签: javascript angular error-handling modal-dialog inject


【解决方案1】:

我不会使用 ngx-modal 我会使用 NgbModal

yazantahhan 的意思是这样的:

import {Injectable} from "@angular/core";
import {NgbModal, NgbModalRef} from "@ng-bootstrap/ng-bootstrap";

@Injectable()
export class ErrorModalService {

  title: string;
  buttonTitle = "OK";
  type: "error";

  protected modalRef: NgbModalRef;

  constructor(protected modalService: NgbModal) {}

  public show(title: string, message: string) {
    this.title = title;
    this.modalRef = this.modalService.open(
      message
    );
  }

  hide() {
    if (this.modalRef) {
      this.modalRef.close();
    }
  }
}
然后像这样注入并使用它:

import { Component } from "@angular/core";
import {ErrorModalService} from "./ErrorModalService";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  title = "testAngular";

  constructor(
    private errorModalService: ErrorModalService,
  ) {}


  showError() {
    this.errorModalService.show("title", "message");
  }
}

别忘了在你的模块中提供服务

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import {ErrorModalService} from "./ErrorModalService";
import {BsModalService} from "ngx-bootstrap/modal";
import {NgbModule} from "@ng-bootstrap/ng-bootstrap";

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    NgbModule,
  ],
  providers: [
    ErrorModalService,
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

【讨论】:

  • 谢谢你帮我清理了。那么剩下的唯一问题是 - 我是否失去了为模态设置样式的能力。现在,模态在组件中的原因是,它的样式很重。我应该把 HTML 和 CSS 放在哪里?
  • 不,一点也不。您仍然可以将模态功能封装在自己的组件中(如 ng-bootstrap.github.io/#/components/modal/examples 提供的示例)并使用该服务进行错误处理。您唯一需要做的就是将 modalComponent 注入到服务中,就像上面示例中将服务注入到 AppComponent 中一样。 (当然,您还必须在模块中提供组件)
猜你喜欢
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2015-12-22
  • 1970-01-01
  • 2017-01-06
  • 2021-07-15
相关资源
最近更新 更多