[英]How to conditionally set a [disabled] attribute value in an Angular Reactive Form?
[英]Angular 6 Reactive Form - how to set conditional value?
我在項目中使用Angular 6和Reactive Form。 我有一個關於如何設置條件的問題,例如,如果數據為空則不轉換為日期,則將其保留為空。
this.editForm = this.fb.group(
{
firstName: [this.user.firstName, Validators.required],
lastName: [this.user.lastName, Validators.required],
workingDate: [
moment(this.user.workingDate).format("DD/MM/YYYY"),
Validators.required
], ....
}
我想放置一個條件,例如this.user.workingDate ==“”,然后將其保留為空(如果我們使用moment(),它將改為顯示當前日期)
怎么做 ?
你可以嘗試一下:
workingDate: [
this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : null,
Validators.required
], ....
使用三元運算符,您可以設置如下條件
workingDate: [
(this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : ''), Validators.required
]
this.editForm = this.fb.group(
{ workingDate: [this.user.workingDate ? moment(this.user.workingDate).format("DD/MM/YYYY") : null, Validators.required],
mobileNo: [this.user.mobileNo ? this.user.mobileNo : null, [Validators.maxLength(11), Validators.minLength(10)]]
}
從“瞬間”導入*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.