【问题标题】:unique nested ng-repeat populated from database从数据库填充的唯一嵌套 ng-repeat
【发布时间】:2018-01-26 06:20:22
【问题描述】:

我正在尝试为嵌套的 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>

这是控制器

    $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 的价格。

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

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

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

    【讨论】:

    • 您好,我不想更改我想读取的数组
    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2019-04-18
    相关资源
    最近更新 更多