gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:33:03 27 4
gpt4 key购买 nike

当查看 ngx-bootstrapsource code这里:

modal-options.class.ts

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

使用方法是什么?

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

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

例如在使用服务之前:

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

这样,我认为我们可以将自定义 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

最佳答案

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

重要:由于模态元素在呈现的HTML中位于组件元素之外,因此应关闭组件的CSS封装,或者在另一个中指定类的样式属性文件,以确保样式应用于模态元素。

下面的代码片段可以在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显示了从外部文件导入 CSS 样式到 styles.css 的示例,允许在组件中保留 CSS 封装。

关于css - 使用 ngx-bootstrap modalService 时添加自定义类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47957036/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com