【问题标题】:Angular Custom Directive CssAngular 自定义指令 Css
【发布时间】:2019-07-02 02:04:56
【问题描述】:

尝试在 Angular 中创建自定义指令时遇到了一些问题。这是我的html代码:

 <div class="col-12 col-md-6 col-lg-6">              
    <label [hidden]="!initservicesService.invoiceCurrencyLblVisible" for="">Invoice Currency</label>
       <select class="form-control" name="currencyCd" [hidden]="!initservicesService.invoiceCurrencyComboBoxVisible" [(ngModel)]="billProfileDO.currencyCd" id="" [disabled]="initservicesService.invoiceCurrencyComboBoxEnabled" [required]="initservicesService.invoiceCurrencyComboBoxCompulsory"                     (ngModelChange)="invoiceCurrencyComboBox_valueChanged($event)">
            <option *ngFor="let item of invoiceCurrencyList" value="{{item.cdValue}}">{{item.decodeValue}}</option>
        </select>
</div>

然后,在我的“必需”指令类中,我尝试将“*”添加到每个必需属性的标签中:

import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
    selector: '[required]'
})
export class RequiredDirective {
    constructor(public el: ElementRef) {
        this.el = el;
    }
    // tslint:disable-next-line: use-life-cycle-interface
    ngOnInit() {
        this.formRequiredFunctions();
    }

    public formRequiredFunctions() {
        const that = this.el.nativeElement;
        console.log(that)
        const className: any = this.el.nativeElement.className;
        if (className === 'form-control') {
            $(that)
                .prev('label')
                .after('<span class="required"> *</span>');
            $(that)
                .prev('.form-tooltip')
                .prev('label')
                .after('<span class="required"> *</span>');
            $(that)
                .parent('.input-group')
                .prev('label')
                .after('<span class="required"> *</span>');
        } 
    }
}

但是,问题是,对于那些具有“必需”和“隐藏”属性的字段,“*”仍然显示,因此它不应该显示。有什么想法吗?

谢谢!

【问题讨论】:

  • 您是要向&lt;select&gt; 下拉列表中的元素或“发票货币”标签中添加“*”吗?使用 Angular 和 jquery 也只是在问问题。
  • @nash11 是的,我正在尝试将“*”添加到标签中。在这种情况下你有什么推荐的方法吗?

标签: angular directive


【解决方案1】:

您不需要为 required 创建自定义指令,因为 Angular 已经涵盖了这一点(请参阅 here)。将其与ngIf 一起使用 指令(参见here)在标签上显示“*”。

<div class="col-12 col-md-6 col-lg-6">
  <label [hidden]="!initservicesService.invoiceCurrencyLblVisible" for="currencyCd">
    Invoice Currency<span *ngIf="initservicesService.invoiceCurrencyComboBoxCompulsory"> *</span>
  </label>
  <select class="form-control" name="currencyCd" id="currencyCd"
    [hidden]="!initservicesService.invoiceCurrencyComboBoxVisible"
    [disabled]="initservicesService.invoiceCurrencyComboBoxEnabled"
    [required]="initservicesService.invoiceCurrencyComboBoxCompulsory"
    (ngModelChange)="invoiceCurrencyComboBox_valueChanged($event)" [(ngModel)]="billProfileDO.currencyCd">
    <option *ngFor="let item of invoiceCurrencyList" value="{{item.cdValue}}">
      {{item.decodeValue}}
    </option>
  </select>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 2018-11-15
    • 2023-04-04
    相关资源
    最近更新 更多