输入类型无线电检查页面加载后重置

时间:2017-07-27 14:38:31

标签: angular

我在forloop模板中输入了radio类型,如下所示:

    <ng-template ngFor [ngForOf]="numberOfOptions | reverse" let-item>
        <input id="rating{{item}}" type="radio" name="rating" value="{{item}}" [(ngModel)]="alreadySelected" [checked]="item === defaultSelected">
        <label for="rating{{item}}">{{item}}</label>
    </ng-template>

其中defaultSelected是我的组件中的属性:

defaultSelected: number

当页面重新加载时,选择了正确的无线电但突然之后它只会重置为未选择的无线电。

1 个答案:

答案 0 :(得分:0)

该属性定义为:

set alreadySelected(value) {
    this._alreadySelected = value
    this.notify.emit(value);  
}

但是没有吸气剂所以我添加了这样的吸气剂:

get alreadySelected(): number {
    return this._alreadySelected
}

然后像这样检查条件:

<input id="rating{{item}}" type="radio" name="rating" [value]="item" [(ngModel)]="alreadySelected" [checked]="item == alreadySelected">

然后我有一个defaultSelected,因为它是可选的:

ngOnInit(): void {
    if (this.defaultSelected) {
        this.alreadySelected = this.defaultSelected
    }
}