首页 > 解决方案 > 如何用角度格式化密钥?

问题描述

我正在尝试从 spotify API 格式化主题演讲

我们

如何将此数字格式化为标准音级符号?

 <div class="key-note">{{trackFeature.key}}</div>

标签: angular

解决方案


由于key将是 的固定值0 <= key < 12,因此最快的方法是将符号列表(?)定义为数组(可能在外部文件中,例如config.ts)。

配置文件

export const NOTATIONS = [ 
  'C', 
  'C♯/D♭', 
  'D', 
  'D♯/E♭', 
  'E', 
  'F', 
  'F♯/G♭', 
  'G', 
  'G♯/A♭', 
  'A', 
  'A♯/B♭', 
  'B' 
];

资料来源:维基

并在组件中使用

控制器

import { Component } from '@angular/core';
import { NOTATIONS } from './config';

export class AppComponent  {
  notations = NOTATIONS;
  ...
}

模板

<div class="key-note">{{ notations[trackFeature.key] || 'Key unavailable' }}</div>

推荐阅读