【问题标题】:How to display a value from JSON in AngularJS如何在 AngularJS 中显示来自 JSON 的值
【发布时间】:2019-11-29 12:23:11
【问题描述】:

如何在 AngularJS 中显示来自 JSON 数据的值。我正在调用 API 来获取 JSON 数据。

我要显示的一些值的示例:

"Example": [{
    "Source": "Something Here",
    "Value": "1"
}, {
    "Source": "Something Else",
    "Value": "2"
}, {
    "Source": "Another One",
    "Value": "3"
}],

该示例显示了 JSON 数据的传递方式。如何显示“something here”的值和/或显示“something else”的值

显示没有问题

"Test1": "Value 1",
"Test2": "Value 2",
"Test3": "Value 3",

使用函数:

function fetch() {
  $http.get("https://www.example.com/exampleData.json")
    .then(function(response) {
      $scope.display = response.data;
    }); 
} 

并显示它:

<div ng-if="display.Response==='True'">
{{ display.Test1 }}
</div>

希望我对自己的解释足够好,因为我对 Angular 还很陌生,但到目前为止我很喜欢它!

【问题讨论】:

标签: json angularjs


【解决方案1】:

想通了:

"Example": [{
"Source": "Something Here",
"Value": "1"
}, {
"Source": "Something Else",
"Value": "2"
}, {
"Source": "Another One",
"Value": "3"
}],

示例是一些嵌套的 JSON 数据。

要显示一个值,您可以从示例中这样做:

<div>Show it to me: {{display.Example[1].Source}}</div>

将输出:

Show it to me: Something Else

正如我之前所说,我是 angularjs 的新手,但你能做的事情真是太棒了。

【讨论】:

    【解决方案2】:

    我没有放弃理解你到底是什么问题,但我可以尝试猜测。 也许您想显示数组中的所有值:

    "Example": [{
        "Source": "Something Here",
        "Value": "1"
    }, {
        "Source": "Something Else",
        "Value": "2"
    }, {
        "Source": "Another One",
        "Value": "3"
    }],
    

    你可以用 ng-repeat 做到这一点,我给你做了一个小例子: https://jsfiddle.net/cowhazj0/3/

    希望对你有帮助。

    【讨论】:

    • 对不起,如果我的问题不够清楚。我是新手 :) 该示例显示了我的 JSON 数据是如何传递的。例如,我如何显示“something here”的值和/或显示“something else”的值
    【解决方案3】:

    <div ng-repeat="item in display.Example"> {{item.Source}} : {{item.Value}} </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-12
      • 2015-10-06
      • 2018-03-16
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      相关资源
      最近更新 更多