如何解决为什么ng-repeat只返回列表的一个值两次?
<div class="row" ng-repeat="product in products">
<div class="col-sm-12 col-md-3">
<div class="form-group">
<input type="text" name="warehouseName"
ng-model="warehouseName"
ng-readonly="true"/>
</div>
</div>
....
</div>
$scope.getAllProductsByWarehouseId = function() {
productService.getAllProductsByWarehouseId($scope.product.id).$promise
.then(function (response) {
$scope.products = response;
console.log("response",$scope.products[0]);
console.log("response",$scope.products[1]);
for(let i=0; i<=$scope.products.length; i++) {
$scope.products = response;
console.log("response",$scope.products[i]);//However I have a list,this return one value
this.products.push(response);
}}).catch(function(e) {
console.log('Error: ',e);
throw e;
}).finally(function() {
console.log('This finally block');
});
}
这是我项目的一部分。当对象产品是具有3个对象的列表时。带有控制台日志
console.log(“ response”,$ scope.products [0]);
console.log(“ response”,$ scope.products [1]);
我可以看到物体..正确!!!!!!
但是在网络浏览器中,我得到了相同对象的两倍。.有什么办法纠正它吗?
解决方法
不确定我是否完全理解您的问题,但是您两次将api响应分配给$ scope.products。
$scope.products = response; //here you assign it after the promise
console.log("response",$scope.products[0]);
console.log("response",$scope.products[1]);
for(let i=0; i<=$scope.products.length; i++) {
$scope.products = response; // and again here.
console.log("response",$scope.products[i]);//However I have a list,this return one value
this.products.push(response); // not sure what this does
}
,
您的代码中有几个问题,主要的问题是将响应分配给$ scope.products,这看起来没有必要。您应该使用数组来初始化$ scope.products,并始终将项目推入其中,而不是覆盖整个列表。
$scope.getAllProductsByWarehouseId = function() {
$scope.products = []
productService.getAllProductsByWarehouseId($scope.product.id).$promise
.then(function (response) {
for(let i=0; i<=$scope.products.length; i++) {
// $scope.products = response;
console.log("response",this return one value
this.products.push(response);
}}).catch(function(e) {
console.log('Error: ',e);
throw e;
}).finally(function() {
console.log('This finally block');
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。