【发布时间】:2018-10-17 08:41:30
【问题描述】:
我正在尝试列出活动设备。将此信息保存在实例变量screens 中。每当我使用setTimeOut 进行更改时,它都会起作用,但是如果我在callback 部分进行更改,它就不起作用。
代码如下:
import { Component } from "@angular/core";
import { NavController } from "ionic-angular";
import { Zeroconf } from '@ionic-native/zeroconf';
@Component({
selector: "page-home",
templateUrl: "home.html"
})
export class HomePage {
screens: Array<{name: any, ip: any}> = [];
constructor(public navCtrl: NavController, private zeroconf: Zeroconf) {
this.screens.push({
name: "TEST",
ip: "123"
})
setTimeout(() => {
this.screens.push({
name: "TEST2",
ip: "Test2"
})
}, 1000);
setTimeout(() => {
this.screens.push({
name: "TEST3",
ip: "Test3"
})
}, 2000);
setTimeout(() => {
this.screens.push({
name: "TEST4",
ip: "Test4"
})
}, 10000);
this.zeroconf.watch('_http._tcp.', 'local.').subscribe(result => {
if (result.action == 'added') {
this.screens.push({ //This is not working
name: result.service.name,
ip: result.service.ipv4Addresses
});
setTimeout(() => {
this.screens.push({
name: result.service.name,
ip: result.service.ipv4Addresses
});
}, 1000);
} else {
console.log('service removed', result.service);
}
});
}
}
这可能是什么原因?
【问题讨论】:
-
你能把你的HTML代码贴在这里吗?
标签: angular ionic-framework ngfor