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