0
只是一个简单的问题,如果有人知道如何解决这个问题,但我有一些复选框输入问题。Angular2 ngModel复选框未定义
它通过以下方式
<input type="checkbox" [(ngModel)]="settings.ht_enabled" (ngModelChange)="changeSetting('ht_enabled')" id="setting-ht_enabled"> <label for="setting-ht_enabled">ht enabled</label>
每次配置我点击我的模型的值是不确定的复选框,并(ngModelChange)
被触发只做一个console.log
。 设置得到正确设置,我看到选中的复选框。只有当我实际点击它时,我的模型的值才会变得不明确。
另一个需要注意的是,这个Angular2项目是作为一个web worker运行的。
有没有人有类似的问题,并知道如何解决这个问题?
这块HTML背后的组件是
@Component({
selector: 'app-setting-form',
templateUrl: './setting-form.component.html',
styleUrls: ['./setting-form.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SettingFormComponent {
settings: Setting;
constructor(
private service: SettingService,
private cdr: ChangeDetectorRef,
private messageBrokerService: MessageBrokerService,
) {
this.messageBrokerService.send('access_token', '', this.initialize.bind(this));
}
initialize(token) {
this.service.getSettings(token).subscribe(settings => this.handleSettings(settings));
}
handleSettings(settings: any) {
this.settings = new Setting(settings);
this.cdr.markForCheck();
}
changeSetting(setting: string) {
console.log(this.settings);
}
}
你可以发布一个有点您的JavaScript的? –
你有一个对象“设置”在你的JavaScript? –
你试过初始化'settings'吗?看来它没有任何价值。 –