首页 > 解决方案 > 如何根据 Firebase 中的值存储显示用户输出(Angular)

问题描述

在我的 Firebase 中,状态存储为“0”、“1”、“2”、“3”。当我获取数据时,它以这种方式呈现“状态:0”。我如何对其进行编码,以便当我获取数据时它将显示“状态:新”而不是“状态:0”

组件.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>

组件.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);
    });
  });
}

标签: angularfirebase

解决方案


您可以?在插值中使用三元运算符,如下所示:

<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;
}

组件.html

  <td>{{getStatus(content.status) }}</td>

推荐阅读