【问题标题】:How to check whether a property exists in a JObject in an Angular App如何检查 Angular 应用程序中的 JObject 中是否存在属性
【发布时间】:2017-09-01 12:08:10
【问题描述】:

我需要检查给定属性是否存在于 JObject 中,以便与我的 Angular 应用程序的 ng-show 指令一起使用。

我尝试使用方括号语法访问该属性:

<li ng-show="@Model["punkt1"] != null">@Model.punkt1</li>

这是 JSON 响应的示例:

{
  "value": null,
  "editor": {
    "name": "Infobox",
    "alias": "infobox",
    "view": "/App_Plugins/MyGridEditor/infobox.html",
    "render": "/App_Plugins/MyGridEditor/infobox.cshtml",
    "icon": "icon-grid",
    "config": {}
  },
  "active": false,
  "heading": "Toiletter",
  "punkt1": "qwert",
  "punkt2": "qwert"
}

“标题”属性之后最多可以有 5 个属性,我只需要显示接收到的属性,这意味着如果 JSON 响应中不存在该属性,则应删除或隐藏 li 元素。

我上面的方法仍然没有隐藏 li 元素,我无法对不存在的属性进行任何检查。

不要介意标题前的属性。

【问题讨论】:

    标签: c# angularjs json properties conditional-statements


    【解决方案1】:

    应该是

    <li ng-show="@Model.punkt1">@Model.punkt1</li>
    

    【讨论】:

    • 那仍然显示没有内容的 li 元素。 i.imgur.com/A3Sgrnx.png
    • 你能用调试看看你对这些项目有什么价值吗?可能有一些空格。
    • 啊,我想我明白发生了什么。这是在我的空 li 元素上返回的 HTML:
    • 它没有任何东西可以与 null 进行比较,因此条件返回 true。
  • 我有没有其他方法可以检查对象中是否存在属性,如果不是则返回 false?
  • 我在上面输入的内容应该可以做到。能不能用console.log(@model)看看你加载的时候是什么样子的?
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签