我知道这个问题有几个线程,但建议的修复对我不起作用。数据正确显示然后很快消失。
据我了解,需要激活一轮变更检测。我用过:
ngAfterViewInit() {
this.cdr.detectChanges();
}
但错误仍然存在。如果我将 this.cdr.detectchanges() 调用添加到 increment() 方法,它会产生一个循环并超出最大调用堆栈。
组件.html:
<div *ngFor="let item of messageSplit[increment()]">
<td>{{item}}</td>
</div>
相关组件.ts:
ngAfterViewInit() {
this.cdr.detectChanges();
}
setContainer(container: string) {
this.messageContainer = container;
}
loadMessages() {
this.messageSplit = [];
const currentUserId = +this.authService.decodedToken.nameid;
this.userService.getMessages(this.authService.decodedToken.nameid,
this.messageContainer)
.do(messages => {
_.each(messages, (message: Message) => {
if (this.messageContainer !== 'unread') {
if (this.compareArray(message) !== true) {
this.splitMessages(message);
}
}
if (message.isRead === false && message.recipientId === currentUserId)
{
this.userService.markAsRead(currentUserId, message.id);
}
});
}, error => {
console.log('');
}, () => {
console.log(this.messageSplit);
this.splitReady = true;
})
.subscribe((res: Message[]) => {
this.messages = res;
});
}
compareArray(message) {
let count = 0;
for (let i = 0; i < this.messageSplit.length; i++) {
if (message.content !== null) {
if (this.messageSplit[i] === message.content.split(',')) {
count = count + 1;
}
}
}
if (count > 0) {
return true;
}
return false;
}
splitMessages(message) {
if (message.content !== null) {
this.messageSplit.push(message.content.split(','));
}
}
increment() {
return this.counter++;
}
我的目标是让每个子项目显示在表格中