css - Angular2 根据组件选择器名称应用 css

标签 css angular sass ng2-bootstrap ngx-bootstrap

我使用的是 ngx-bootstrap datepicker,它有 daypicker、monthpicker 和 yearpicker 作为内部组件。我想将 css 应用于 daypicker 中存在的表。

<custom-datepicker>

<datepicker> //ngx-bootstrap datepicker component
    <daypicker>
        <table>
            //rows, want to apply some css to only this table
        </table>
    </daypicker>
    <monthpicker>
        <table>
            //rows
        </table>
    </monthpicker>
    <yearpicker>
        <table>
            //rows
        </table>
    </yearpicker>
</datepicker>

</customdatepicker>

CustomDatePickerComponent.ts

@Component({
    selector: 'custom-datepicker',
    templateUrl: 'custom-datepicker-component.html',
    styleUrls: ['custom-datepicker-component.scss'],
})

export class CustomDatePickerComponent {

}

custom-datepicker-component.html

<datepicker></datepicker>

自定义日期选择器组件.scss

//I want to apply this css to a table which is inside daypicker. As of now it's applying to all the tables
table {
    border: 1px solid lightgrey;
}

最佳答案

您可以向表格中添加一个 css 类:

<datepicker> //ngx-bootstrap datepicker component
    <daypicker>
        <table class="new-class">
            //rows, want to apply some css to only this table
        </table>
    </daypicker>
...

然后使用常规类选择器:

.new-class {
    // your styling
}

或者像这样向您的日期选择器或日期选择器添加一个类:

<datepicker class="new-class">
...

并使用后代选择器:

.new-class table {
    // your styling
}

关于css - Angular2 根据组件选择器名称应用 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43961098/

相关文章:

html - 输入 HTML 之间的距离

html - 如何让移动导航菜单以横向模式显示?

Angular 2 在不使用本地存储的情况下记住我的功能

angular - 有什么方法可以防止以 Angular 破坏组件?

ruby-on-rails - Rails 3.2.12 应用程序 sass/compass @import 仅在生产中失败

css - 如何在 ruby​​ on rails 中使用 scss partials 使用变量

html - 为什么我无法渲染 rc-slider 及其标 checkout 现在一行中?

html - 个人资料图片默认大小

angular - 在 tsconfig.json 上使用路径无法使用 Angular 解析

javascript - 如何停止在 Angular md2 输入文本区域控件中同时应用所需的 scss 和 ng-invalid 类 scss