2017-11-14 20 views
0

私は、変数が 'id'と言うコンポーネントを持っています。このコンポーネントでは、パラメータとしてその変数(id)を使用するサービスを呼び出しています。 私のテンプレートにラジオボタンの値を割り当てます(id)。 問題は..私は "ngOnInit()"で私のサービスを呼び出しているので、ラジオボタンの値をとらないで "id"の初期値をとります。私のコンポーネントでデータバインディング角度4

:私のテンプレートで

import { Component, OnInit } from '@angular/core'; 
import { BartersService } from '../../core/services/barters.service'; 

export class BarterViewComponent implements OnInit { 
id:string=""; 
allCategories ; 
constructor(private barServ: BartersService) { } 
    ngOnInit() { 
    this.barServ.findByPage(this.id) 
    .subscribe(respone => { 
    this.allCategories = respone.data; 
    }); 

    } 
onSelectionChange(val) { 
    this.id = val; 
} 

} 

ここ.. は私のコードです
<div *ngFor="let cat of allCategories ; let i=index;">        
    <input style="margin-bottom: 10px;" type="radio" #R 
     (change)="onSelectionChange(R.value)" name="rad" value="cat['id']"> 
     {{cat['name']}} 
</div> 

対象:

ターゲットは、ラジオボタンの選択に基づいてビューを更新しています。

+0

予想される動作は何ですか? –

答えて

0

にこの

value="cat['id']" 

あなたはこれを試すことができます。

export class BarterViewComponent implements OnInit { 
id:string=""; 
allCategories ; 

constructor(private barServ: BartersService) { } 
ngOnInit() { 
    refreshCategories() 
} 

onSelectionChange(val) { 
    this.id = val; 
    refreshCategories() 
} 

refreshCategories() { 
    this.barServ.findByPage(this.id).subscribe(respone => { 
     this.allCategories = respone.data; 
    }); 
} 
} 
+0

ありがとうございます。 –

+0

idをパラメータとしてrefreshCategories()に渡すこともできます。 –

2

変更

[value]="cat['id']"またはvalue="{{cat['id']}}"

+0

お返事ありがとうございます。 –