简体   繁体   English

如何将CSS样式传递给Angular 2组件?

[英]How to pass CSS styles to my Angular 2 component?

I've built an Angular2/4 component that is, basically, a masked date input. 我已经构建了一个Angular2 / 4组件,该组件基本上是一个蒙版的日期输入。 I use it in place of a textbox input, and have some code behind it to treat date conversions. 我用它代替了文本框输入,并在其后有一些代码来处理日期转换。 It works well enough, but now i want to apply a CSS style and have it changing the input. 它工作得很好,但是现在我想应用CSS样式并更改输入。

I want to write <app-date-input class='someCssClass'></app-date-input> and that class be attributed to my internal input. 我想编写<app-date-input class='someCssClass'></app-date-input>并将该类归因于我的内部输入。

My code for the component follows: 我的组件代码如下:

date-input.component.html date-input.component.html

import { Component, Input, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { DatePipe } from "@angular/common";
import * as moment from 'moment';

date-input.component.ts date-input.component.ts

import { AppConstantsService } from "../../_services";

@Component({
    selector: 'app-date-input',
    templateUrl: './date-input.component.html',
    styleUrls: ['./date-input.component.css'],
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => DateInputComponent),
        multi: true
    }]
})
export class DateInputComponent implements ControlValueAccessor {
    @Input()
    public valor: Date;

    @Input()
    public readonly: boolean;

    public dataString: string;

    constructor(public appConstants: AppConstantsService) {
    }

    atribuirData(dataEntrada: string) {
        if (!dataEntrada || dataEntrada == '') {
            this.valor = null;
            this.propagateChange(this.valor);
            return;
        }

        let parts = dataEntrada.split('/');

        try {
            let newDate = moment(parts[2] + '-' + parts[1] + '-' + parts[0]).toDate();
            // let newDate = new Date(+parts[2], +parts[1]-1, +parts[0]);

            this.valor = newDate;
            this.propagateChange(this.valor);
        } catch (error) {
            // return dataEntrada;
            console.log(error);
        }
    }

    writeValue(value: any) {
        this.valor = value;
        const datePipe = new DatePipe('pt-BR');
        this.dataString = datePipe.transform(this.valor, 'dd/MM/yyyy');
    }

    propagateChange = (_: any) => { };

    registerOnChange(fn) {
        this.propagateChange = fn;
    }

    registerOnTouched() {
    }
}

Although there are multiple ways to achieve this 尽管有多种方法可以实现这一目标

  1. Setting styles in the Child Component 在子组件中设置样式

    <app-date-input [childClass]='someCssClass'></app-date-input>

Now in DateInputComponent, create an @Input() property and in the HTML for the component, you can do something like 现在,在DateInputComponent中,创建一个@Input()属性,然后在该组件的HTML中,执行以下操作

'<input [class]="input Variable Name">'

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

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