【问题标题】:Display value of an array that has numbers as it's key (within an ng-repeat)显示以数字为键的数组的值(在 ng-repeat 中)
【发布时间】:2016-02-24 12:41:37
【问题描述】:

我的控制器中有以下数组数组(script.js 文件):

angular.module('myApp', [])
  .controller('oneController', ['$scope', function($scope) {
    $scope.countries = [{
      "name": "Aruba",
      "code": "ABW",
      "1960": 54208,
    }];

在我的 index.html 文件中,我正在运行一个 ng-repeat 来显示一个包含国家名称、代码和 1960 年人口的表格。

<body ng-app="myApp">
  <h1>Challenge Three</h1>
  <div ng-controller="oneController">
    <table>
      <tr ng-repeat="country in countries">
        <td>{{ country.name }}</td>
        <td> {{ country.code }}</td>
        <td> {{ country.1960 }}</td>
      </tr>

country.name 和 country.code 显示在表中,但 country.1960 没有。我必须对“1960”使用某种语法才能使其正常工作吗?

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    您需要使用方括号访问它:

    {{ country["1960"] }}</td>
    

    【讨论】:

    • 你是个传奇!谢谢。
    【解决方案2】:

    您还应该能够使用以下内容:

    <table>
    <tr ng-repeat="(key,val) in countries[0]">
    <td ng-bind="key"></td>
    </tr>
    </table>
    

    【讨论】:

    • 这不会产生相同的输出......将有 3 行而不是 3 列,并且将输出键而不是值
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2018-09-01
    • 1970-01-01
    相关资源
    最近更新 更多