我以一种我不太喜欢的方式解决了问题,欢迎任何更好的解决方案。谢谢。
@Pipe({
name: 'iso2date'
})
export class Iso2Date {
transform(value: any, args: Array<any>): string {
return moment(value).format("YYYY-MM-DD");
}
}
@Component({
selector: 'foo',
directives: [Calendar],
pipes: [Iso2Date],
template: `
...
<p-calendar dateFormat="yy-mm-dd"
[ngModel]="model | iso2date"
(ngModelChange)="model=date2iso($event)"></p-calendar>
...
`})
class FooComponent {
@Input() model;
date2iso(value) {
var m = moment(value, "YYYY-MM-DD");
return m.toISOString();
}
}
ps:管道在动作中不起作用,否则以下内容会更清洁
@Pipe({
name: 'date2iso'
})
export class Date2Iso {
transform(value: any, args: Array<any>): string {
return moment(value,"YYYY-MM-DD").toISOString();
}
}
...
(ngModelChange)="model=(event | date2iso)"></p-calendar>
...
但是我收到以下错误:
Parser Error: Cannot have a pipe in an action expression at column 20 in [model=($event | date2iso)] in FooComponent@19:28 ("="yy-mm-dd"
[ngModel]="model | iso2date"
[ERROR ->](ngModelChange)="model=($event | date2iso)"