样的问题:http://plnkr.co/edit/7FeRoyyqDnjXpV9Q9Vpy?p=preview如何获取我的Angular2 DatePipe格式的日期以更新?
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2>{{myDate}}</h2> <!-- THIS UPDATES AS EXPECTED -->
<h2>{{myDate | date: 'longDate'}}</h2> <!-- THIS DOES NOT -->
<a (click)="prevMonth()" href="javascript:;">Previous Month</a>
<a (click)="nextMonth()" href="javascript:;">Next Month</a>
</div>
`,
})
export class App {
myDate: Date;
constructor() {
this.myDate = new Date();
}
nextMonth() {
this.myDate.setMonth(this.myDate.getMonth() + 1);
}
prevMonth() {
this.myDate.setMonth(this.myDate.getMonth() - 1);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
当我通过我的变量,而无需使用任何管道,它更新符合市场预期。但是同一个变量的DatePipe格式副本不会更新。管道是否仅更新观测值?或者我可以使用这种标准的日期类型,并期望它实时更新?
我没有看到DatePipe API中的任何内容表明这是预期的行为,但我已经缩小到只有DatePipe可能以这种方式影响行为的程度。 https://angular.io/docs/ts/latest/api/common/index/DatePipe-pipe.html