【问题标题】:ng-repeat is showing data that is not thereng-repeat 显示不存在的数据
【发布时间】:2017-08-16 11:57:16
【问题描述】:

我正在通过函数动态使用数据加载,然后使用 ng-repeat 填充。

下面是控制器代码。

$scope.loadproducts = function(item_id) {

        var pdata = $.param({
            item_id : item_id,
        });

        $http({
          method: 'POST',
          url: baseurl + 'api/get_items_in_category_cart',
            data: pdata,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            $scope.items = response.data.products;
            $scope.description = response.data.description;
            console.log($scope.items);
             $timeout(apply_tooltip, 500);
        }, function errorCallback(response) {

        });
    }; 

查看

<div class="container col-md-6" ng-repeat="item in items">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h4 class="panel-title">{{ item.title }}</h4>
        </div>
        <div class="panel-body">
            <div class="col-md-3 col-xs-5"><a href="#" class="thumbnail"><img ng-src="{{ item.thumbnail }}"></a></div>
            <div class="col-md-9" style="">
                <div class="row">
                    <div class="col-md-12">
                        <span style="font-size: 14px; font-weight: 500;">{{ item.description }}</p>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right" style=" text-align: left;">
                        <p>{{item.price | currency:"NZ $"}}</p>
                    </div>
                    <div class="col-md-8" style="  text-align: left;">

                        <input type="text" value="{{item.qty}}" style="width:50px; height: 28px; line-height: 28px;" readonly id="qty_{{item.id}}"/>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

在初始页面加载时,代码会执行它应该执行的操作。 有一个继续按钮,用户被带到另一个页面。 如果用户返回,项目应该重新加载。

问题是在重新加载期间,应该显示每个项目的数量的文本框 ->> value="{{item.qty}}" 似乎对所有 qty 文本框都显示相同的数量,而不管原来的数量。

console.log($scope.items) 显示了它应该具有的正确值。只是显示器显示的数量不对。

有什么想法吗?

【问题讨论】:

  • 你的 apply_tooltip 是做什么的?
  • 悬停时显示工具提示
  • 你可以试试 wrap $scope.items = response.data.products; $scope.description = response.data.description;也进入 $timeout(0)?
  • 对不起,我迷路了。你能举个例子吗
  • 我怀疑这可能是因为相同的id,请尝试在ng-repeat="item in items track by item.id" 中添加track by

标签: javascript angularjs


【解决方案1】:

如果您的数据加载正确,您可以通过两种方式:

1 - 尝试在您的输入中使用 ng-model

<input type="text" ng-model="item.qty" style="width:50px; height: 28px; line-height: 28px;" readonly id="qty_{{item.id}}"/>

您可以删除“只读”属性并添加 ng-disabled="true"。

2 - 您可以在获得新值之前尝试清除列表。因此,在从后端再次加载值之前,您可以清除数据:

$scope.items = [];

请求完成后再放数据。

请注意,第二个解决方案不是常规解决方案,应该在您找不到其他解决方案的情况下使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2020-08-16
    • 1970-01-01
    • 2016-02-22
    • 2013-03-04
    • 2016-09-04
    • 1970-01-01
    相关资源
    最近更新 更多