1
我对Angular 2.0中的双向绑定有个疑问。 在我看来,我显示了选定的日期(月和年)+本月的天数。 有按钮可以进入下一个或上个月。 但是,当我点击它们显示日期不更新,但它已成功更新模型中。并在本月的日子正确更新(与ngFor)。 有没有人知道在视图中更新日期的正确方法?Angular 2.0中的双向绑定日期
查看:
<span>
{{date | date:'MMMM yyyy'}}
</span>
<button (click)="goToPreviousMonth()">Previous month</button>
<button (click)="goToNextMonth()">Next month</button>
<div *ngFor="#week of weeks">
<!-- Show days -->
</div>
组件:
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, NgModel} from 'angular2/common';
@Component({
selector: 'monthview',
templateUrl: './monthview.html',
directives: [CORE_DIRECTIVES]
})
export class MonthView {
weeks:any = [];
date:Date;
public goToMonthFromCurrent(addMonth:number) : void {
// Update date
this.date.setMonth(this.date.getMonth() + addMonth);
// Some other code to calculate days of month...
// This will update the date in view
this.date = new Date(this.date.getTime());
}
public goToNextMonth() : void {
this.goToMonthFromCurrent(1);
}
public goToPreviousMonth() : void {
this.goToMonthFromCurrent(-1);
}
}
谢谢。 'this.date = new Date(this.date.getTime());'确实是解决方案(有问题的更新解决方案)。解决方案将如何通知角度更改? – user2276975