离子2:样式离子选择与验证

时间:2017-08-07 18:11:08

标签: javascript html css angular ionic-framework

我尝试设置<ion-select>输入标签的样式,但无法使其正常工作。似乎ionic会自动将ng-invalid类添加到ion-select DOM元素,但输入的<label>是DOM的上游,所以我无法设置样式。

我的HTML

<ion-item>
    <ion-label>Type of Weigh In</ion-label>
    <ion-select [(ngModel)]="check_in.type_of_weighin" name="type_of_weighin" interface="action-sheet" required #type_of_weighin="ngModel">
        <ion-option value="standard">Standard</ion-option>
        <ion-option value="LM">Limited Maintenence</ion-option>
        <ion-option value="FM">Full Maintenence</ion-option>
    </ion-select>
</ion-item>

Renderd DOM:

<ion-item class="item item-block item-ios item-select input-has-value">
    <div class="item-inner">
        <div class="input-wrapper">
            <ion-label class="label label-ios" id="lbl-103">Type of Weigh In</ion-label>
            <ion-select interface="action-sheet" name="type_of_weighin" required="" class="select select-ios ng-untouched ng-pristine ng-invalid">
                <div class="select-placeholder select-text"></div>
                <div class="select-icon">
                    <div class="select-icon-inner"></div>
                </div>
                <button aria-haspopup="true" class="item-cover" ion-button="item-cover" id="sel-103-0" aria-labelledby="lbl-103" aria-disabled="false" style="transition: none;"></button>
            </ion-select>
        </div>
    </div>
    <div class="button-effect"></div>
</ion-item>

是否有其他方式来设置<label>

的样式

1 个答案:

答案 0 :(得分:1)

鉴于离子重新格式化,我不知道用css直接做的方法,但你可以附加一些eventListeners并根据更改进行更新。

<强> HTML

<ion-item>
    <ion-label [class.is-invalid]="inputsInvalid.type_of_weighin">Type of Weigh In</ion-label>
    <ion-select [(ngModel)]="check_in.type_of_weighin" name="type_of_weighin" interface="action-sheet" required #type_of_weighin="ngModel" (change)="checkValid($event)">
        <ion-option value="standard">Standard</ion-option>
        <ion-option value="LM">Limited Maintenence</ion-option>
        <ion-option value="FM">Full Maintenence</ion-option>
    </ion-select>
</ion-item>

<强> TS

inputsInvalid = {};

checkValid(e) {
    const el = e.target;
    inputsInvalid[el.id] = el.classList.contains('ng-invalid');
}

<强> SCSS / CSS

ion-label.is-invalid {
    ...
}

希望这是有道理的。