2016-12-13 8 views
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ワーカーとして実行されています。

誰も似たような問題を抱えており、これを解決する方法を知っていましたか?

HTMLのこの作品の背後にあるコンポーネントは、チェックボックスの値がAngular2で正しくngModelを設定していないようだと私は迅速な回避策を作った

@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); 
    } 
} 
+0

を追加し、あなたのjavascriptのビットを投稿することができますか? –

+0

javascriptにオブジェクト "settings"がありますか? –

+0

'settings'を初期化しようとしましたか?これまで価値がないようです。 –

答えて

0

です。私はこれがWebワーカーの中で実行されているプロジェクトと関係があるかどうかわかりませんが、次のように動作しています。

<input type="checkbox" [(ngModel)]="settings.ht_enabled" (click)="changeSetting('ht_enabled')" id="setting-ht_enabled"> <label for="setting-ht_enabled">ht enabled</label> 

とchangeSettings機能が

changeSetting(setting: string) { 
    this.settings[setting] = !this.settings[setting]; 
    console.log(this.settings); 
} 

である私は(ngModelChange)を削除し、私自身のクリックイベント

関連する問題