我現在是組件捕獲數據,我想通過服務Angular2:通過服務
的Component1(開始)將它發送到另一個組件從組件將數據發送到一個組件:無線電箱 查看
<md-radio-button
*ngIf="item.id===1"
value="{{item.value}}"
class="{{item.class}}"
checked="{{item.checked}}"
(click)="onSelectType1(item)"> //here i'm catching the value
{{item.label}}
</md-radio-button>
TS碼
public onSelectType1(selected:any){
this.formeService.type1Change(selected.nb)
}
SERVICE:
@Injectable()
export class FormeService{
public type1S : any;
public type1Change(selected):any
{
this.type1S=selected; //here i'm catching it into the service
}
組分2:(完):簡單查看
TS碼
export class ContentComponent{
constructor(public BackService : BackgroundService ,
public formeService: FormeService)
{
console.log(this.formeService.type1S)
////// that diplays me in the console that it's undefined !!!!!!!!!!!!!!!!!
The probleme is HERE : how may i access to the value of my variable in this part
}
!!!!!!! !並在視圖中的同一時間,它顯示我的價值:
{{formeService.type1S}} // this works normally
誰可以告訴我,我怎樣才能在「結束組件TS碼」顯示我的數據的價值?????
如果你不認爲這是一個重複添加評論,把它重新打開。 –
問題是如何重新使用我的數據在ts代碼中的值不在視圖中,使用主體可以傳遞數據並將其顯示在視圖中但這裏的上下文是如何從代碼的一部分顯示它 – firasKoubaa
部分使用'.subscribe('將在TypeScript中獲得服務中值的更改通知。 –