Dependency injection Angular2与来自服务的数据绑定
我有这个组件Dependency injection Angular2与来自服务的数据绑定,dependency-injection,angular,2-way-object-databinding,angular2-template,angular2-services,Dependency Injection,Angular,2 Way Object Databinding,Angular2 Template,Angular2 Services,我有这个组件 @组件({ templateUrl:'./app/component/template/actualstate.template.html', StyleURL:['./app/component/style/actualstate.style.css'], 管道:[动量管道,大写管道] }) 导出类ActualState组件实现OnInit{ 公共房间:客房; 建造商(私人客房服务:客房服务){ roomService.roomSelected$.subscribe(room=>
@组件({
templateUrl:'./app/component/template/actualstate.template.html',
StyleURL:['./app/component/style/actualstate.style.css'],
管道:[动量管道,大写管道]
})
导出类ActualState组件实现OnInit{
公共房间:客房;
建造商(私人客房服务:客房服务){
roomService.roomSelected$.subscribe(room=>this.onRoomSelected(room));
}
onRoomSelected(房间:房间){
这个房间=房间;
控制台日志(“房间”,房间);
}
}
还有另一个组件
@组件({
templateUrl:'./src/admin/template/admin.template.html',
样式URL:['./src/admin/style/admin.style.css'],
提供者:[用户服务]
})
导出类AdminComponent{
建造商(私人客房服务:客房服务){
}
onClick(){
this.roomService.selectRoom(“,”);
this.router.navigate(['ActualState']);
}
}
}
,此服务:
@Injectable()
出口舱位服务{
私有路由_-room=“public/mock/room.json”;
选定的公用房间$:EventEmitter;
构造函数(专用http:http){
this.roomSelected$=新的EventEmitter();
}
public selectRoom(子域:string,id:string){
//挑对房间
让房间=。。。
此.roomSelected$.emit(房间);
}
私有句柄错误(错误:响应){
返回Observable.throw(error.json().error | |'Server error');
}
}
此模板:
<div class="actual-state" *ngIf="room">
<h3>Salle {{ room.name }}
</h3>
</div>
Salle{{room.name}
目的是:
管理组件(用户单击某些按钮)
->Listener OnClick调用service roomService上的方法
->roomService发出一个事件(即公共事件)
->appComponent侦听此事件(.subscribe)
我不知道为什么这不起作用。
从未显示。。即使console.log(文件室)在控制台中显示某些内容
这个数据绑定是如何工作的因为看起来数据不是双向绑定的
编辑:我理解这个问题,它与我的路线有关事实上,我不明白当你改变路线时,路线的组成部分会被破坏这一事实我想你需要订阅
return this.http.get(this.route_room)
.map(res => res.json())
.do(data => {
this.roomSelected$.emit(data);
})
.subscribe(value => {})
.catch(this.handleError);
我想你需要订阅
return this.http.get(this.route_room)
.map(res => res.json())
.do(data => {
this.roomSelected$.emit(data);
})
.subscribe(value => {})
.catch(this.handleError);
@Julestroung事实上比这更复杂,我编辑了我的代码我看到了你的编辑,但不明白为什么现在更复杂。你试过我的建议了吗?你有什么错误吗?我应该在哪里订阅?我不是在等待http请求被执行,我在等待一些用户事件,在本例中是一个clickWhat's
this.http.get(…)
code,那么在您的问题中,它与您的问题无关吗?忘记http.get代码,我复制粘贴了错误的code@julestruong其实要比这复杂得多,,我编辑了我的代码我看到了你的编辑,但不明白为什么它现在更复杂了。你试过我的建议了吗?你有什么错误吗?我应该在哪里订阅?我不是在等待http请求被执行,我在等待一些用户事件,在本例中是单击this.http.get(…)
代码,那么在您的问题中,什么时候与您的问题无关?忘记http.get代码,我复制粘贴了错误的代码