更改年份时更改值复选框

时间:2018-09-04 08:54:11

标签: angular

我使用的是Angular2,我在复选框中选择了2017年,当我更改2018年时,我在12月中有支票,我需要在更改年份时,所有复选框都未选中。

通过此选择,我可以控制年份:

<select name="year" (ngModelChange)="onChangeYear()" >
...

输入示例,一月:

 <div class="checkbox" [ngClass]="{'disabled': checkCanEnabled(1)}">
 <input type="checkbox" id="enero" name="enero" [disabled]="checkCanEnabled(1)  || disabledPeriod" [(ngModel)]="selectedjanuary" (ngModelChange)="onChangeMonths()" >

.ts组件:

// I get the values from my Service.

    public selectedjanuary: boolean = this.data.months.jan;
    public selectedfebruary: boolean = this.data.months.feb;
    public selectedmarch: boolean = this.data.months.mar;
    public selectedapril: boolean = this.data.months.apr;
    public selectedmay: boolean = this.data.months.may;
    public selectedjune: boolean = this.data.months.jun;
    public selectedjuly: boolean = this.data.months.jul;
    public selectedaugust: boolean = this.data.months.aug;
    public selectedseptember: boolean = this.data.months.sep;
    public selectedoctober: boolean = this.data.months.oct;
    public selectednovember: boolean = this.data.months.nov;
    public selecteddecember: boolean = this.data.months.dec;

    public onChangeYear = function () { 
 // I set the values from my service.
        this.data.months.jan = false;
        this.data.months.feb = false;
        this.data.months.mar = false;
        this.data.months.apr = false;
        this.data.months.may = false;
        this.data.months.jun = false;
        this.data.months.jul = false;
        this.data.months.aug = false;
        this.data.months.sep = false;
        this.data.months.oct = false;
        this.data.months.nov = false;
        this.data.months.dec = false;
    }

但是当我更改年份时,选中了从2017年12月到2018年12月的日期...

1 个答案:

答案 0 :(得分:1)

复选框绑定到selectedjanuary ... selecteddecember。它们未绑定到this.data.months.jan ... this.data.month.dec,因此,将后者设置为false不会更改复选框的值。

在编写public selectedjanuary: boolean = this.data.months.jan;时,您将获取this.data.months.jan(而不是 reference ),并将其放在selectedjanuary中。两者之间没有链接,因此修改this.data.months.jan不会不会再次修改selectedjanuary