在我的Firebase中,状态存储为“ 0”,“ 1”,“ 2”,“ 3”。当我获取数据时,将以“状态:0”的形式显示。如何编码,以便在获取数据时将显示“状态:新”而不是“状态:0”
component.html
<html>
<table id="table" class="table table-striped">
<thead>
<tr>
<th>Due Date</th>
<th>Assigner</th>
<th>Assignee</th>
<th>Urgency</th>
<th>Description</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let content of acts">
<td>{{content.dueDate}}</td>
<td>{{content.Assigner}}</td>
<td>{{content.Assignee}}</td>
<td>{{content.urgency}}</td>
<td>{{content.description}}</td>
<td>{{content.status}}</td>
</tr>
</tbody>
</table>
</html>
Component.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormArray } from
"@angular/forms";
import { TodoItem } from "../_models";
import { ToDoListService } from '../_services';
@Component({
selector: "toDoList",
templateUrl: "./toDoList.component.html",
styleUrls: ["./toDoList.component.css"]
})
export class ToDoList implements OnInit {
Form: FormGroup;
currentUser: User;
acts: Array<any> = [];
act: TodoItem;
constructor(private fb: FormBuilder,
private todoSrv: ToDoListService) {
let res2do = todoSrv.getAll();
res2do.then(result2do => {
result2do.subscribe(async _acts => {
this.acts = _acts;
console.log("Initial act", this.acts);
});
});
}
答案 0 :(得分:0)
您可以在插值中使用?
三元运算符,如下所示:
<td>{{(content.status === 0) ? 'New' : content.status }}</td>
如果您有多个状态,则应在component.ts中创建一个方法,使用switch语句根据状态号设置不同的值,如下所示:
getStatus(statusNum) {
let status = '';
switch(statusNum) {
case 0:
status = 'new';
break;
case 1:
status = 'pending';
break;
case 2:
status = 'completed';
break;
}
return status;
}
component.html
<td>{{getStatus(content.status) }}</td>
答案 1 :(得分:0)
有几种方法可以实现
<td>{{content.status === 0 ? 'new' : content.status }}</td>
或
TS
getStatus(status: number): string {
if(status === 0) {
return 'new';
}
//you can add more user output here
}
HTML
<td>{{ getStatus(content.status)}}</td>