【发布时间】:2016-07-24 08:51:33
【问题描述】:
我正在从一个组件中捕获数据,并尝试通过服务将其发送到另一个组件
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)
}
服务:
@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 代码中而不是在视图中重新使用我的数据的值,使用主题可以传递数据并在视图中显示它但是这里的上下文是如何显示它代码部分
-
.subscribe(的部分是在 TypeScript 中获取服务中值更改的通知。 -
this.formeService.type1S.subscribe( type =>this.testVariable= type); console.log(this.testVariable) =======> 未定义
-
同时:在视图中 {{testVariable}} 完美运行
标签: typescript angular angular2-routing angular2-directives angular2-services