我现在是组件捕获数据,我想通过服务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中获得服务中值的更改通知。 –