I从json文件中上传数组 每1.5秒我检查文件是否有任何变化(此刻我在一个文件上测试没有任何变化),但是当我检查,如果为什么在Angular 2中两个相等的对象显示'不相等'
if (this.itemsParentArray[i] !== this.itemInArray[i])
它总是显示,这不是平等的,执行console.log(“”不等于“)
难道我错过了在代码的东西?那就是:
export class HomeComponent {
itemsParentArray = [];
itemInArray = [];
myRes: Content;
showAssigned:boolean = false;
constructor(private structureRequest: StructureRequestService) {
setInterval(() => {
this.timerGetStructure();
}, 1500);
}
// using with setInterval to get new data and sets into content Array with this.updateItems(result) if it's new
timerGetStructure() {
this.structureRequest.sendRequest().subscribe((result) => this.updateItems(result));
}
updateItems(result) {
this.myRes = result;
this.itemInArray = this.myRes.content;
for (let i = 0; i < this.itemInArray.length; i++) {
if (this.itemsParentArray[i] !== this.itemInArray[i]) {
// this.itemsParentArray[i] = this.itemInArray[i];
console.log("not equal");
}
}
}
//
ngOnInit() {
//makes http request and puts result into parentArray after 3 sec.
this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));
}
//view items
viewNodes(result) {
setTimeout(() => {
this.myRes = result;
this.itemsParentArray = this.myRes.content;
this.showAssigned = true;
}, 3000);
}
}
正如你看到它从同一个文件加载数据(我不改变文件中的数据!):
this.itemsParentArray = this.myRes.content;
和(每1.5秒):
this.itemInArray = this.myRes.content;
谢谢。将开始工作。 – Serhiy