【问题标题】:Way to add custom class when using ngx-bootstrap modalService使用 ngx-bootstrap modalService 时添加自定义类的方法
【发布时间】:2018-06-06 00:34:51
【问题描述】:

在这里查看ngx-bootstrap source code 时:

modal-options.class.ts

有一个可选的class property 定义为class?: string;

它的使用方法是什么?

是否可以添加自定义类,例如:

this.modalService.config.class = 'myClass';

在使用servive之前例如:

this.modalRef = this.modalService.show(template, {
  animated: false
});

这样,我想我们可以在显示的modal中添加自定义CSS

我尝试添加自定义类但没有成功。

那个类属性不是数组,如果适用,是不是意味着我们只能添加一个自定义类?

演示:通过添加和覆盖modal 类,模式不显示

https://stackblitz.com/edit/ngx-bootstrap-3auk5l?file=app%2Fapp.component.ts

以这种方式添加 modal 类没有帮助:

this.modalRef = this.modalService.show(template, Object.assign({},
                this.config, { class: 'gray modal-lg modal' }));

https://stackblitz.com/edit/ngx-bootstrap-awmkrc?file=app%2Fapp.component.ts

【问题讨论】:

  • 我将您的 CSS 从 app.component.css 移动到 styles.css 并且我看到了一些不同的东西。你试过吗?
  • 那个css在哪里?上面的demo链接不能修改,可以先fork一下。
  • 试试这个:stackblitz.com/edit/…
  • 可能很有趣,但已经在这里看到了:plnkr.co/edit/NJRvUMZN2kHZruFgeOjQ?p=preview,在我的一个问题中,看起来一样,但是当必须在复杂的应用程序中添加它以使每个组件都具有正确的功能时,它可能会更好css。不确定这是否是我们目前唯一可以实施的方式。
  • 我刚刚添加了 justify 并对齐到我正在玩的 stackblitz styles.css ......死点。看起来不错。你现在有你需要的一切吗?

标签: css angular ngx-bootstrap ngx-bootstrap-modal


【解决方案1】:

根据 ngx-bootstrap documentation 关于 Modal 组件(参见 component 选项卡),您可以将 class 成员添加到 config 对象。

重要:由于modal元素在渲染的HTML中是在组件元素之外的,所以应该关闭组件的CSS封装,或者类的样式属性应该在另一个中指定文件,以确保将样式应用于模态元素。

下面的代码sn-p可以在this stackblitz中执行。

import { Component, TemplateRef, ViewEncapsulation } from '@angular/core';
import { BsModalService, BsModalRef } from 'ngx-bootstrap';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class AppComponent {
  modalRef: BsModalRef;
  config = {
    animated: true,
    keyboard: true,
    backdrop: true,
    ignoreBackdropClick: false,
    class: "my-modal"
  };

  constructor(private modalService: BsModalService) { }

  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template, this.config);
  }
}

使用这样的 CSS 文件:

.my-modal {
  border: solid 4px blue;
}

.my-modal .modal-header {
  background-color: lime;
}

.my-modal .modal-body {
  background-color: orange;
}

更新:此other stackblitz 显示了从外部文件导入styles.css 的CSS 样式示例,允许将CSS 封装保留在组件中。

【讨论】:

  • 好的,但是正如您所见,要为该库获得一个工作示例并不容易,我正在寻找一个具体示例,因为所有这些模式都没有显示居中,我正在尝试这样做,当您尝试在具有 modal 类的开发工具中添加实时样式时,您可以在中心获得一点模式,但不幸的是,没有关于此的具体示例
  • 我修改了你的 stackblitz 以使 CSS 样式适用于 my-class 类。我只应用了颜色,但你可以理解。
  • 感谢您所做的努力,encapsulation: ViewEncapsulation.None 可能会为我必须添加模板的组件创建一些样式问题,可能我会考虑创建一个模态组件分开查看。稍后会做一些测试并给出价值和一些反馈。
  • 另一种方法是在单独的 CSS 文件中为 my-modal 类定义样式属性,然后将其导入代码文件:import "./modal-class.style.css";
  • ConnorsFan:在答案中,您能否添加一个 stackblitz 链接来删除 encapsulation: ViewEncapsulation.None 并添加一个 css 文件以获得相同的结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-18
  • 2021-11-09
  • 2020-12-12
相关资源
最近更新 更多