【发布时间】:2017-08-04 20:28:56
【问题描述】:
我在我的 Angular2 项目中使用ng2-select2,我想添加一些样式,例如宽度、高度、边框半径、字体粗细、字体大小……但我无法管理它。
我的 Html 代码是这样的:
<select2 [data]="exampleData"></select2>
我也尝试将 class 和 id 添加到我的 select2 元素中,但它不起作用。你知道我该怎么做吗?
【问题讨论】:
我在我的 Angular2 项目中使用ng2-select2,我想添加一些样式,例如宽度、高度、边框半径、字体粗细、字体大小……但我无法管理它。
我的 Html 代码是这样的:
<select2 [data]="exampleData"></select2>
我也尝试将 class 和 id 添加到我的 select2 元素中,但它不起作用。你知道我该怎么做吗?
【问题讨论】:
如果你想使用“宽度”,你可以将它与 Select2Options 类一起使用。
在你的组件中只定义一个 Select2Options 对象:
let options: Select2Options;
设置你想要的属性:
this.optionsSelect = {
placeholder: "Select option...",
allowClear: true,
width: "100%"
}
在您的 html 中,将 [options] 输入与您的选项名称一起输入:
<select2 [data]="seccion" [cssImport]="true" (valueChanged)="onSeccionChange($event)" [options]="optionsSelect"></select2>
使用这个类你可以自定义一些东西,例如:添加一个占位符,或者允许保持选择而不选择。
就我而言,我正在尝试更改背景颜色,但找不到解决方案。
【讨论】:
dropdownCss: {backgroundColor: 'red'}
您可以尝试自定义组件
@Component({
selector: "your-component",
templateUrl: "your-template.component.html",
styles: [`
:host >>> .select2-container {
min-width: 1000px;
}
`]
})
【讨论】:
你必须像这样使用回购描述中提到的width属性
<select2 [width]="dynamic_width"></select2>
参考这里
https://github.com/NejcZdovc/ng2-select2#inputs
否则您可以使用ngStyle 或ngClass 动态绑定样式(PS:- 未测试)
【讨论】: