[英]Angular 7+ Using CustomPipe on ngModel
I have an input with a date calendar picker that is storing dates on a server and returning them onInit
. 我有一个带有日期日历选择器的输入,该输入将日期存储在服务器上并在
onInit
上返回它们。 The problem I am having is when the date is not stored or picked, it returns 01/01/0001 numbers. 我遇到的问题是,当日期没有存储或选择时,它返回01/01/0001数字。 I want the input to be empty or have a placeholder like dd/mm/yyyy if nothing was selected or saved on the server yet.
我希望输入为空,或者如果服务器上未选择任何内容或将其保存,则有一个占位符,如dd / mm / yyyy。 So I built a custom pipe which I can't get it to work with
ngModel
. 因此,我建立了一个自定义管道,使其无法与
ngModel
一起ngModel
。
I am getting an error of value is undefined . 我收到一个值未定义的错误 。
HTML: HTML:
<div class="form-group">
<label for="motDate">MOT Date</label>
<input type="text" class="form-control" placeholder="dd-mm-yyyy" ngbDatepicker #d="ngbDatepicker" (focus)="d.open()" #motDate>
<input type="hidden" [ngModel]="model.motDate | dateNotEmpty" (ngModelChange)="onMOTDateSelected($event)" name="motDate" />
</div>
Component Typescript: 组件打字稿:
export class VehicleFormComponent implements OnInit {
@ViewChild('motDate') motDatePicker;
ngOnInit() {
// shows the dates from the server in the right format using moment
this.motDatePicker.nativeElement.value = `moment(this.model.motDate).format('DD/MM/YYYY');`
}
onMOTDateSelected(e) {
this.model.motDate = new Date(e.year.toString() + '-' + `e.month.toString() + '-' + e.day.toString());`
}
}
Custom Pipe: 自定义管道:
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'dateNotEmpty',
pure: false
})
export class DateNotEmptyPipe extends DatePipe implements PipeTransform {
transform(value: any, format: string): any {
if (value.indexOf('0001') > -1) {
return "";
} else {
return new DatePipe('en-GB').transform(value, format);
}
}
}
I think this would work. 我认为这会起作用。
Here are your codes: 这是您的代码:
//html
<div class="form-group">
<label for="motDate">MOT Date</label>
<input type="text" class="form-control" placeholder="dd-mm-yyyy"
ngbDatepicker #d="ngbDatepicker" (focus)="d.open()" #motDate>
<input type="hidden" [ngModel]="model.motDate | dateNotEmpty" (ngModelChange)="onMOTDateSelected($event)" name="motDate" />
</div>
//typescript
export class VehicleFormComponent implements OnInit {
@ViewChild('motDate') motDatePicker;
ngOnInit() {
// shows the dates from the server in the right format using moment
this.motDatePicker.nativeElement.value = `moment(this.model.motDate).format('DD/MM/YYYY');`
}
onMOTDateSelected(e) {
this.model.motDate = new Date(e.year.toString() + '-' + `e.month.toString() + '-' + e.day.toString());`
}
}
//custom pipe
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
@Pipe({
name: 'dateNotEmpty',
pure: false
})
export class DateNotEmptyPipe extends DatePipe implements PipeTransform {
transform(value: any, format: string): any {
console.log('Value here': value);
if (value && value.indexOf('0001') > -1) {
return "";
} else {
return new DatePipe('en-GB').transform(value, format);
}
}
}
Thanks for opening my eyes Sunny in the comments above, I had this pipe already used on a few places so wanted to reuse it here and got stuck for no reason. 感谢您在上面的评论中让Sunny睁开眼睛,我已经将此管子在一些地方使用过,因此想在这里重复使用它,并且无缘无故地卡住了。 I added an if statement now after the date has been initialised and it works fine:
在日期初始化之后,我现在添加了一个if语句,它可以正常工作:
Thanks to others as well for trying to help me out on my journey, much appreciated! 也感谢其他人在旅途中为我提供的帮助,非常感谢!
this.motDatePicker.nativeElement.value = moment(this.model.motDate).format('DD/MM/YYYY');
if (this.motDatePicker.nativeElement.value.indexOf('0001') > -1) {
this.motDatePicker.nativeElement.value = "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.