Angular2的新功能。當我啓動我的應用程序時,我的模板顯示connected
爲false
。然後控制檯記錄connected to socket.io
,但connected
在我的模板中仍然顯示爲false。我如何設置事件,以便在連接狀態發生變化時connected
將在我的模板中正確讀取?角度2更新模板var?
import {Component, OnInit} from '@angular/core';
let io = require('socket.io-client');
let socket = io.connect('http://localhost:4300');
@Component({
selector: 'my-app',
template: require('./app.component.pug'),
})
export class AppComponent implements OnInit{
connected = false;
ngOnInit(){
socket.on('connect',()=> {
this.connected = true;
console.log('connected to socket.io');
})
}
getSocketStatus(){
console.log(this.connected);
}
}