AngularJS-ng如果运行摘要循环
我在加载视图时面临无限循环的问题。使用控制器中的ngResource从API调用加载数据。在正确渲染视图之前,视图似乎被重新加载多次。我在调用scope方法的模板中使用ng指令,这似乎进入了循环,导致视图被重新呈现 这是我的控制器AngularJS-ng如果运行摘要循环,angularjs,angularjs-digest,Angularjs,Angularjs Digest,我在加载视图时面临无限循环的问题。使用控制器中的ngResource从API调用加载数据。在正确渲染视图之前,视图似乎被重新加载多次。我在调用scope方法的模板中使用ng指令,这似乎进入了循环,导致视图被重新呈现 这是我的控制器 .controller('IndexCtrl', ['$scope', '$stateParams', 'ProfileInfo', function($scope, $stateParams, ProfileInfo) { $scope.navTitle =
.controller('IndexCtrl', ['$scope', '$stateParams', 'ProfileInfo',
function($scope, $stateParams, ProfileInfo) {
$scope.navTitle = 'Profile Information';
$scope.data = {};
ProfileInfo.query({
id: $stateParams.id
}).$promise.then(function(Profile) {
if (Profile.status == 200) {
$scope.data.Profile = Profile.data[0];
}else{
console.log(Profile.status);
}
}, function(error) {
console.log(error);
});
$scope.showImageBlock = function(object, image) {
if (object.hasOwnProperty('type') && object.type == 'image') {
imageReference = object.value;
var imageUrl;
angular.forEach(image, function(value, key) {
if (value.id == imageReference) {
$scope.data.imageUrl = value.graphic.url;
return;
}
});
}
return object.hasOwnProperty('type') && object.type == 'image';
};
$scope.showText = function(object) {
console.log('text');
return object.hasOwnProperty('type') && object.type == 'text';
};
}
])
这是我的模板
<ion-view cache-view="false">
<ion-nav-title>
{{navTitle}}
</ion-nav-title>
<div class="bar bar-subheader bar-light">
<h2 class="title">{{navSubTitle}}</h2>
</div>
<ion-content has-header="true" padding="true" has-tabs="true" class="has-subheader">
<div ng-repeat="profileInfo in data.Profile">
<div class="list">
<img ng-if="showImageBlock(profileInfo,data.Profile.images)" ng-src="{{ data.imageUrl }}" class="image-list-thumb" />
<div ng-if="showText(profileInfo)">
<a class="item">
{{profileInfo.name}}
<span ng-if="profileInfo.description.length != 0"class="item-note">
{{profileInfo.description}}
</span>
</a>
</div>
</div>
</div>
</ion-content>
{{navTitle}}
{{navSubTitle}}
ngResource调用的实际结果在数组中只有9个项,但它循环了9次以上,并且有多个循环。这种情况会发生一段时间,然后停止。谁能给我指一下修理它的正确方向吗
谢谢最后,我创建了一个自定义指令,该指令在没有触发摘要循环的观察程序的情况下执行ng if的功能。这不是一个很好的解决方案,但它似乎按预期完成了工作。我复制了ng if的代码并删除了$scope watcher。这是定制指令
angular.module('custom.directives', [])
.directive('customIf', ['$animate',function($animate) {
return {
multiElement: true,
transclude: 'element',
priority: 600,
terminal: true,
restrict: 'A',
$$tlb: true,
link: function($scope, $element, $attr, ctrl, $transclude) {
var block, childScope, previousElements;
value = $scope.$eval($attr.customIf);
if (value) {
if (!childScope) {
$transclude(function(clone, newScope) {
childScope = newScope;
clone[clone.length++] = document.createComment(' end customIf: ' + $attr.customIf + ' ');
block = {
clone: clone
};
$animate.enter(clone, $element.parent(), $element);
});
}
}
else {
if (previousElements) {
previousElements.remove();
previousElements = null;
}
if (childScope) {
childScope.$destroy();
childScope = null;
}
if (block) {
previousElements = getBlockNodes(block.clone);
$animate.leave(previousElements).then(function() {
previousElements = null;
});
block = null;
}
}
}
};
}]);
这允许我们使用customIf,如下所示
<div custom-if="showText(profileInfo)">
您可以提供或创建一个plunker,以便我们可以看到日志活动。视图中的函数也会对每个摘要循环求值,因此如果您在另一个地方执行调用摘要循环的操作,您的视图将被更新,视图中的所有函数都将被删除execute@Grundy这是codepen的链接,但我无法在codepen中复制该问题。有没有办法确定问题的原因?