【发布时间】: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>');
}
}
}
但是,问题是,对于那些具有“必需”和“隐藏”属性的字段,“*”仍然显示,因此它不应该显示。有什么想法吗?
谢谢!
【问题讨论】:
-
您是要向
<select>下拉列表中的元素或“发票货币”标签中添加“*”吗?使用 Angular 和 jquery 也只是在问问题。 -
@nash11 是的,我正在尝试将“*”添加到标签中。在这种情况下你有什么推荐的方法吗?