调用ng-erepeat内部的API函数,提示错误.达到10$digest()迭代.在angularjs中中止
Call API function inside ng-repeat, prompting error. 10 $digest() iterations reached. aborting in angularjs
我在控制器中定义了一个作用域函数,该函数调用API从数据库中获取数据。我在ng repeat中使用这个scope函数。但当我运行应用程序时,它会挂起。
视图:
<div class="col-xs-4 product-padding" ng-repeat="product in productList | filter:query | orderBy:'name'| offset: currentPage*itemsPerPage | limitTo: itemsPerPage " ng-cloak>
<div class="theme-04-scope">
<div class="thumbnail">
<small class="defaultImageSize" ng-if="checkImageAttribute(product.ItemAttributes) == true">
<img class="ui-corner-all img-responsive defaultImageSize" ng-src="data:image/jpeg;base64,{{loadProductImage(product.ItemAttributes)}}" />
@*ng-src="/Product/LoadProductImage/{{loadProductImage(product.ItemAttributes)}}?width=200&height=144"*@
</small>
</div>
</div>
</div>
Angularjs控制器:
// Get product image.
$scope.loadProductImage = function (itemAttributes) {
var imageId = 0;
$.each(itemAttributes, function (index, data) {
if (data.AttributeId == 1000700 && data.DataXml != null) {
imageId = data.DataXml;
return false;
}
});
productRepository.getProductImage(imageId, 200, 144).then(function (imageArrary) {
$scope.itemIdArr.push(imageId);
$scope.productImage = imageArrary;
});
return $scope.productImage;
}
存储库功能:
getProductImage: function (imageId, width, height) {
var deferred = $q.defer();
$http.post('/Product/LoadProductListImage', JSON.stringify({ id: imageId, width: width, height: height })).success(deferred.resolve).error(deferred.reject);
return deferred.promise;
}
当您将函数放置在视图插值内时,每个摘要周期至少会对其求值两次,每秒可以求值多次。因此,将API调用放入其中一个函数中不是一个好主意。
.directive('getProductImage', function ($http) {
return {
scope: {
imageId: '@imageId',
width: '@width',
height: '@height'
},
link: function (scope, element, attrs) {
console.info(scope.imageId, scope.width, scope.height)
$http.post('/Product/LoadProductListImage', JSON.stringify({ id: scope.imageId, width: scope.width, height: scope.height })).success(function (data) {
scope.imageArrr = data;
});
},
template: '<img class="ui-corner-all img-responsive defaultImageSize" ng-src="data:image/jpeg;base64,{{imageArrr}}"></img>'
}
});
它会出现在你的视野中,看起来像这样:
<span get-product-image width="200" image-id="200" height="144"></span>
享受。。
相关文章:
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- AngularJS:我可以跳过函数参数回调吗
- AngularJS UI路由器不能像ng路由器那样工作
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 如何包含特定于每个视图angularjs的javascript文件
- AngularJS:ng之后,重复$scope值未按预期更新
- AngularJS错误:$rootScope:infdig Infinite$digest循环:达到10次$digest
- angularjs 无限$digest 当范围没有变化时循环
- AngularJS 错误:$rootScope:infdig 无限$digest循环
- AngularJS 10 $digest() 迭代在列表显示时达到
- AngularJS v1.2.x $digest() 无限循环指令隔离范围对象参数
- AngularJs:错误:用于配对项目的自定义筛选器引发错误:“达到 10 次 $digest() 迭代.流产!
- AngularJS orderBy与ngModel相同的属性引发$digest错误
- AngularJS:未捕获错误:已达到[$rootScope:infdig]10$digest()迭代
- 根据AngularJS中$apply或$digest循环的指令更新模型
- 调用ng-erepeat内部的API函数,提示错误.达到10$digest()迭代.在angularjs中中止
- $digest已经运行- AngularJS
- AngularJS:在$httpBackend.flush()之前需要$digest
- angularjs的ngRepeat指令不会触发列表中其他项的digest
- AngularJS - 10次$digest()迭代完成.流产