【问题标题】:How should I implement the model interface when one of the identifiers in the response is a number in an Angular app当响应中的标识符之一是 Angular 应用程序中的数字时,我应该如何实现模型接口
【发布时间】:2026-01-03 02:45:02
【问题描述】:

来自 api 的示例响应:

"results":[
      {
         "id":16228,
         "name":"ABCD",
         "preview":"/d8a/d8a61a3a12e52114afdbc28f2c813f5c.jpg",
         "data":{
            "480":"https://mdb.net/steam/6693661/movie480.mp4",
            "max":"https://mdb.net/steam/6693661/movie_max.mp4"
         }

我的界面如下:

interface Movie {
  data: {
    max: string;
    480: string;
  };
}

当我尝试时

<video
        class="movie"
        controls
        *ngFor="let trailer of results"
      >
        <source src="{{ trailer.data.480 }}" <-------- Error here
         type="video/mp4" />
        Your browser does not support the video tag.
      </video>

NG5002:解析器错误:[{{ trailer.data.480 }}] 中的第 14 列出现意外令牌“0.48”

如何解决?

当问号出现时,我如何修复 {{ trail.data?['480'] }}

谢谢

【问题讨论】:

    标签: angular interface response numeric angular12


    【解决方案1】:

    你试过{{ trailer.data['480'] }}

    【讨论】:

    • 您应该删除点,并使用单引号,就像答案所暗示的那样。 trailer.data['480']
    • 为了帮助您输入,您应该在Movie 界面中执行类似['480']: string 的操作。
    • 当问号出现时,我该如何修复 {{ trail.data?['480'] }}?
    • 如果数据未定义,这取决于您要做什么。您还可以在大括号内使用三元运算符
    最近更新 更多