我有一个页面,我有两个标签。
HTML:
<mat-tab label="Active">
<mat-icon for="search">search</mat-icon>
<input type="search" name="search" class="search" placeholder="Company">
<ul>
<li *ngFor="let message of activeMessages" (click)="showMessage(message)" [class.activeShow]="message.id == message_id">
<span>{{message.date}}</span>
<img src="{{message.image}}" alt="img" />
<p style="padding-top: 16px;">{{message.name}}</p>
<p ><b>{{message.subject}}</b></p>
</li>
</ul>
</mat-tab>
<mat-tab label="Closed">
<mat-icon for="search">search</mat-icon>
<input type="search" name="search" class="search" placeholder="Company">
<ul>
<li *ngFor="let closemessage of closedMessages" (click)="closeMessage(closemessage)" [class.activeShow]="closemessage.id == message_idc">
<span>{{closemessage.date}}</span>
<img src="{{closemessage.image}}" alt="img" />
<p >{{closemessage.name}}</p>
<p ><b>{{closemessage.subject}}</b></p>
</li>
</ul>
</mat-tab>
TS:
this.service
.getMessages()
.subscribe(
data => {
this.messagesdata = data;
console.log(data);
if(data.active== true) {
this.activeMessages = this.messagesdata
}
},error => {});
}
控制台输出:
答案 0 :(得分:1)
过滤您的服务回复,例如
.subscribe(
data => {
this.messagesdata = data;
let activeMsgs = data.filter(msg => msg.active == true)
.map(m => { return m.messages});
this.activeMessages = [].concat.apply([], activeMsgs);
let closeMsgs = data.filter(msg => msg.active == false)
.map(m => { return m.messages});
this.closedMessages = [].concat.apply([], closeMsgs);
}
答案 1 :(得分:1)
这是您的要求,
收到回复后,只需过滤activeMessages
和closedMessages
并根据条件显示
您将变量声明为this.message_show
,并将tab`` is changed, you make the
消息部分'声明为实现,您可以根据需要获取消息。
<强>组件:强>
this.service
.getMessages()
.subscribe(
data => {
this.messagesdata = data;
let activeMsgs = data.filter(msg => msg.active == true)
this.activeMessages = activeMsgs;
console.log(this.activeMessages);
if (this.activeMessages.length > 0) {
if (!this.message_show) {
this.message_show = this.activeMessages[0];
this.message = true;
this.message_id = this.activeMessages[0].id
}
}
let closeMsgs = data.filter(msg => msg.active == false)
this.closedMessages = closeMsgs;
if (this.closedMessages.length > 0) {
if (!this.message_close) {
this.message_close = this.closedMessages[0];
this.message_idc = this.closedMessages[0].id
}
}
console.log(this.closedMessages);
}, error => { });
答案 2 :(得分:0)
json pipe,这是一个如何显示数据值的示例
{{data | json}}
现在,您可以在控制台中看到数据的值。