角2+:输入装饰并不反映复选框

问题描述 投票:2回答:1

我有一个分量

timeBoxSelector HTML:

<input type="checkbox" [(ngModel)]="selected">

TS:

@Component({
   ...
})
export class TimeboxComponent implements OnInit {
    @Input() selected: boolean;

    constructor() {}

    ngOnInit() {
        console.log('Selected: ', this.selected);
    }
}

现在,当我创建

<app-timebox selected="false"><app-timebox/>
<app-timebox selected="true"><app-timebox/>

在这两种情况下,作为选择的最初显示的复选框。为什么是这样的话,我怎么能解决这个问题?

html css angular checkbox angular2-forms
1个回答
3
投票

在这两种情况下,你要绑定的非空字符串,这是truthy值。使用brackets符号告诉角度所绑定的值将作为JavaScript表达式:

<app-timebox [selected]="false"><app-timebox/>
<app-timebox [selected]="true"><app-timebox/>
© www.soinside.com 2019 - 2024. All rights reserved.