我想在下拉菜单中将当前年份显示为默认日期,当点击 prev 按钮时,它应该将年份减一,并且它应该影响 Angular 2 下拉菜单中的选定年份值
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<h2>Select demo</h2>
<a (click)="changeYear()">Prev</a>
<select [(ngModel)]="selectedyear" (ngModelChange)="onChange($event)" >
<option *ngFor="let c of year" [ngValue]="c"> {{c}} </option>
</select>
`
})
class App {
year = ['2016','2015','2014'];
selectedyear ;
onChange(year) {
alert(year);
this.selectedyear = year;
}
changeYear(){
this.selectedyear = this.selectedyear -1;
this.onChange(this.selectedyear);
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
问题是当您在 2014 年之后单击上一个按钮时,它将是 2013 年,而您在数组年份中没有它。
所以每当你减少检查元素是否存在于数组内时,如果不推送到数组内。
changeYear(){
this.selectedyear = this.selectedyear -1;
this.year.push(this.selectedyear);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句