【问题标题】:JavaScript - Can't read property of undefinedJavaScript - 无法读取未定义的属性
【发布时间】:2017-07-05 19:25:38
【问题描述】:

好的,这可能是个愚蠢的问题,因为我遗漏了一些东西,但我似乎无法弄清楚是什么!

这是我的对象,使用{{ definitionList | json }}

{
  "State": "Success",
  "Data": [
    {
      "Description": "Default",
      "ID": 4,
    },
    {
      "Description": "Hello World",
      "ID": 14,
    },
    {
      "Description": "Test Def",
      "ID": 11,
    },
    {
      "Description": "test definitie",
      "ID": 12,
    },
    {
      "Description": "testffvfvfvffv",
      "ID": 8,
    },
    {
      "Description": "Werknemer_kantoor",
      "ID": 3,
      "Type": 
    },
    {
      "Description": "Werknemer_kantoor",
      "ID": 6,
    }
  ],
  "Error": null
}

好的,现在我想打印出“数据”uisng {{ definitionList.Data | json }}

我收到错误消息

原始异常:无法读取未定义的属性“数据”

我不明白为什么?我错过了什么?

【问题讨论】:

  • 也许您正在异步加载definitionList,而在执行模板时尚未定义definitionList?试试{{ definitionList?.Data | json }}(使用?)。
  • 如果没有更多细节,我们无法为您提供帮助。请使用 Stack Snippets([<>] 工具栏按钮)通过 runnable minimal reproducible example 更新您的问题,以展示问题。
  • 当您尝试{{ definitionList[0].Data | json }} 时会发生什么?
  • @AngularFrance 大声笑,天哪,非常感谢你!今天第二次了!
  • @AngularFrance - 将您的评论剪切并粘贴到答案中,以便 OP 可以勾选它,并且 20 年后任何人都知道有答案。

标签: javascript json angular object


【解决方案1】:

也许您正在异步加载definitionList,并且在执行模板时尚未定义?

试试下面的代码(注意?):

{{ definitionList?.Data | json }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2015-10-26
    • 2013-03-14
    相关资源
    最近更新 更多