Angular.js view function call result
Is there an obvious problem with the following snippet:
<ul id="entry-name" class="breadcrumb">
<li ng-repeat="dir in pathElements()" class="active">
<span ng-show="!$last">
<a href="#!?path={{dir.url}}">{{ dir.name }}</a> <span ng-show="!$first" class="dividier">/</span>
</span>
</li>
<li class="active">{{ pathElements()[pathElements().length - 1].name }}</li>
</ul>
with this js:
$scope.pathElements = function() {
var retval = [];
var arr = $scope.currentPath().split('/');
if (arr[0] == '') {
arr[0] = '/';
}
var url = "/";
retval.push({name: arr[0], url: url});
for (var i = 1; i < arr.length; ++i) {
if (arr[i] != '') {
url += arr[i] + '/';
retval.push({name: arr[i], url: url});
}
}
return retval;
};
This seems to be causing the "Error: 10 $ digest ()" errors. Interrupt! "error, but I'm not sure why. Is it because pathElements () returns a new array every time? Is there a way around this?
+5
2 answers
Yes, this is because you return a new array every time, and the digest loop of the loop is infinite (but Angular terminates it). You must declare it outside the function.
$scope.pathArray = [];
$scope.pathElements = function() {
// retval becomes $scope.pathArray
if (weNeedToRecalcAllPathVariables) {
$scope.pathArray.length = 0;
// ...
}
// ...
}
We use $scope.pathArray.length = 0instead of creating a new one to avoid its continuous operation.
, @Flek. , , . , , $scope.pathArray.
, , , , , , .
+4