javascript - 从数据库中填充的唯一嵌套 ng-repeat

标签 javascript html angularjs

我正在尝试为嵌套的 ng-repeat 获取唯一值

我正在使用两个范围

$scope.AllGames = [ 
   { title: "Doom", productCode: "458544", id_product: "1" }, 
   { title: "Wolvenstein", productCode: "457104", id_product: "2" }, 
   { title: "Quake", productCode: "32542687", id_product: "3" } 
];

$scope.AllPrices= [ 
   { id_productPrice: "1", id_product: "1", price : "599" }, 
   { id_productPrice: "2", id_product: "1", price : "699" }, 
   { id_productPrice: "3", id_product: "2", price : "249" } 
];

这是html

<div ng-repeat="game in AllGames">
    <div>{{game.title}} | {{game.id_product}} | {{game.title}}</div>
    <div ng-init="getPriceByGame(game.id_product)">

    <div ng-repeat="item in AllPrices">
            {{item.price}}
    </div>
</div>

这是 Controller

    $scope.getAllGames = function () {
        var request = 'Games';
        $http({
            method: 'Get',
            url: uri + request
        })
            .success(function (data) {
                $scope.AllGames = data;
           })

    }

    $scope.getPriceByGame = function (id_product) {
    var request = AllPrices/' + id_product;

        $http({
            method: 'Get',
            url: uri + request
        })
            .success(function (data) {
                $scope.AllPrices = data;
            })
    };

所以我的想法是,我将 id_product 发送到数据库,然后我得到该 id 的价格列表,我想为每个独特的产品连接这些价格。现在发生的是 ng-init 工作正常并且请求与 id 一起发送,我收到了该 id 的价格。在控制台日志中,我看到所有价格都按 product_id(所有不同的数组)输入,但我似乎无法在 html 中连接它们,所以我实际上看到了价格。

当然,最后一个请求会覆盖 ng-repeat 中每个游戏的所有价格。我如何保持这种独特性?这样每个产品都可以获得该 ID 的价格。

最佳答案

您可以放置​​另一个 ng repeat 来循环遍历所有价格数组。每次获得新数据时,都会将其推送到数组中。

不要忘记在所有价格的嵌套 ng repeat 中进行编辑,它应该只循环当前索引

关于javascript - 从数据库中填充的唯一嵌套 ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443379/

相关文章:

angularjs - Angular 2 ng-需要

javascript - 如何检测 AngularJS 应用程序的传入请求是 GET 还是 POST 请求?

javascript - 将 JavaScript 与 WTForms 结合使用

javascript - Bootstrap datetimepicker 完全奇怪地定位在不同的 div 上

python - 如何在html页面上显示模型字段 - Django

html - 显示保存在 mysql 数据库中的出版物时,文本从 css 框出现

javascript - ExtJS视口(viewport)产生空白页面

javascript - CckEditor - 同时使用 2 种样式...?

javascript - 使用 javascript 删除主页内容

angularjs - 指令不能 '' 需要 :ngRepeat'