0

我有这个组件

@Component({
    templateUrl: './app/component/template/actualstate.template.html',
    styleUrls: ['./app/component/style/actualstate.style.css'],
    pipes: [MomentPipe, CapitalizePipe]
})
export class ActualStateComponent implements OnInit {
    public room: Room;

    constructor(private roomService: RoomService) {

        roomService.roomSelected$.subscribe(room => this.onRoomSelected(room));
    }

    onRoomSelected(room: Room) {
        this.room = room;
        console.log("room", room);
    }
}

和这个其他组件

@Component({
    templateUrl: './src/admin/template/admin.template.html',
    styleUrls: ['./src/admin/style/admin.style.css'],
    providers: [UserService]
})
export class AdminComponent{

    constructor ( private roomService: RoomService) {
    }

    onClick () {

            this.roomService.selectRoom("","");
            this.router.navigate(['ActualState']);
        }
    }
}

,这项服务:

@Injectable() 
export class RoomService {

    private route_room = "public/mock/room.json";
    public roomSelected$: EventEmitter<Room>;

    constructor (private http: Http) {
        this.roomSelected$ = new EventEmitter();
    }



    public selectRoom (subdomain: string, id: string) {
           // pick the right room
           let room = ...
           this.roomSelected$.emit(room);

    }

    private handleError (error: Response) {
        return Observable.throw(error.json().error || 'Server error');
    }
}

这个模板:

<div class="actual-state" *ngIf="room">
<h3>Salle {{ room.name }}
</h3>
</div>

目的是:管理组件(用户单击某个按钮)-> Listener OnClick 调用服务 roomService 上的方法-> roomService 发出事件(即公共)-> appComponent 监听此事件(.subscribe)

我不知道为什么这不起作用。<h3>即使console.log(room)在控制台中显示一些东西,它也永远不会显示......

这个数据绑定是如何工作的?因为看起来数据不是双向绑定的 ......

编辑:我理解这个问题,它与我所做的路由有关。事实上,我不明白当你改变路线时,路线的组成部分被破坏了

4

1 回答 1

2

我想你需要订阅

return this.http.get(this.route_room)
                    .map(res => res.json())
                    .do(data => {
                        this.roomSelected$.emit(data);
                    })
                    .subscribe(value => {})
                    .catch(this.handleError);
于 2016-02-22T15:02:52.067 回答