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);
}
}