简体   繁体   English

使用ngx-bootstrap modalService时添加自定义类的方法

[英]Way to add custom class when using ngx-bootstrap modalService

When looking to the ngx-bootstrap source code here: 在这里查看ngx-bootstrap 源代码时

modal-options.class.ts 莫代尔,options.class.ts

There is an optional class property defined as class?: string; 有一个可选的class property定义为class?: string; .

What is the way to use it ? 使用它的方法是什么?

Is it possible to add a custom class like: 是否可以添加自定义类,如:

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

Before using the servive as for example: 在使用servive之前,例如:

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

This way, I think we can add custom CSS to the displayed modal 这样,我认为我们可以将自定义CSS添加到显示的模态中

I've tried to add a custom class without success. 我试图添加自定义类但没有成功。

That class property is not an array, if applicable, does it mean that we can only add one custom class ? 该类属性不是数组,如果适用,是否意味着我们只能添加一个自定义类?

Demo: by adding and overriding the modal class, the modal is not showing 演示:通过添加和覆盖modal类,模态不会显示

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

Adding the modal class this way do not help: 以这种方式添加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 https://stackblitz.com/edit/ngx-bootstrap-awmkrc?file=app%2Fapp.component.ts

According to the ngx-bootstrap documentation about the Modal component (see the component tab), you can add a class member to the config object. 根据有关Modal组件的ngx-bootstrap 文档 (请参阅组件选项卡),您可以将class成员添加到config对象。

Important : Since the modal element is outside of the component element in the rendered HTML, the CSS encapsulation should be turned off for the component, or the style attributes for the class should be specified in another file, to make sure that the styles are applied to the modal element. 要点 :由于模态元素在呈现的HTML中的组件元素之外,因此应该为组件关闭CSS封装,或者应该在另一个文件中指定类的样式属性,以确保应用样式到模态元素。

The code snippet below can be executed in this stackblitz . 下面的代码片段可以在这个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);
  }
}

with a CSS file like this: 使用这样的CSS文件:

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

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

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

Update : This other stackblitz shows an example of CSS styles imported from an external file into styles.css , allowing to keep the CSS encapsulation in the component. 更新 :此另一个stackblitz显示了从外部文件导入到styles.css的CSS样式示例,允许将CSS封装保留在组件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM