javascript - Angular JS表中的嵌套ng-repeat

标签 javascript html angularjs

我是 Angular JS 的新手。 我一直在尝试遍历模型集合并将其显示在表格中。

模型看起来像:

var myModule = angular
                .module("myModule", [])
                .controller("myController", function ($scope) {
                    var countries = [
                        {
                            name: "UK",
                            cities: [
                                    {name: "London"},
                                    {name: "Birmingham" },
                                    {name: "Manchestar" }
                            ],
                            flag:"/Images/UK.gif"
                        },
                        {   
                            name: "USA",
                            cities: [
                                    {name: "Los Angeles"},
                                    {name: "Houston"},
                                    {name: "Florida"},
                            ],
                            flag:"/Images/USA.png"
                        }
                    ];
                    $scope.countries = countries;
                });

我希望表结构是

Country City1  City2      City3      Flag
UK      London Birmingham Manchestar .....
USA     ...... .........  .........  .....

但我不能在 html 页面中做同样的事情。

到目前为止,代码如下所示:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  ng-app="myModule">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/MyModuleScript.js"></script>
</head>
<body  ng-controller="myController">
    <div>
        <table>
            <thead>
                <tr>
                    <th>Country</th>
                    <th>City 1</th>
                    <th>City 2</th>
                    <th>City 3</th>
                    <th>Flag</th>
                </tr>
            </thead>
            <tbody ng-repeat="country in countries">
                <tr ng-repeat="city in country.cities">
                    <td>{{ country.name }}</td>
                    <td>
                        {{......}}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

我需要做什么才能达到同样的效果?请解释。

最佳答案

试试这个。您必须将 ng-repeat 放在 td 而不是行上。

var myModule = angular
                .module("myModule", [])
                .controller("myController", function ($scope) {
                    $scope.countries = [
                        {
                            name: "UK",
                            cities: [
                                    {name: "London"},
                                    {name: "Birmingham" },
                                    {name: "Manchestar" }
                            ],
                            flag:"/Images/UK.gif"
                        },
                        {   
                            name: "USA",
                            cities: [
                                    {name: "Los Angeles"},
                                    {name: "Houston"},
                                    {name: "Florida"},
                            ],
                            flag:"/Images/USA.png"
                        }
                    ];
                   
                });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myModule" ng-controller="myController">
    <div>
        <table>
            <thead>
                <tr>
                    <th>Country</th>
                    <th>City 1</th>
                    <th>City 2</th>
                    <th>City 3</th>
                    <th>Flag</th>
                </tr>
            </thead>
            <tbody ng-repeat="country in countries">
                <tr>
                    <td>{{ country.name }}</td>
                    <td  ng-repeat="city in country.cities">
                        {{city.name}}
                    </td>
                  <td><img ng-src="{{country.flag}}"></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

关于javascript - Angular JS表中的嵌套ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238379/

相关文章:

javascript - jQuery/Svg : increment value "dur" when key is pressed

javascript - 通过 Safari 运行 JavaScript 文件

html - 创建一个 3 列横幅,当浏览器调整大小时是动态的,在最小宽度之后它以中间列为中心

css - 如何使用 bootstrap 将浏览器窗口分成两个画面?

javascript - 使用 Javascript 和 Node.js 处理错误 (then/catch)

javascript - 通过键/值引用更新 Javascript 对象值

javascript - 如何从一个div中选择一个元素到另一个?

angularjs - 测试滚动

angularjs - 如何按月份名称排序

javascript - 为什么在使用 javascript/jQuery 添加类时 css 转换不起作用?