Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js watching the result of a function call

Tags:

angularjs

Is there any ostensible issue with the following snippet:

  <ul id="entry-name" class="breadcrumb">
      <li ng-repeat="dir in pathElements()" class="active">
          <span ng-show="!$last">
              &nbsp;<a href="#!?path={{dir.url}}">{{ dir.name }}</a>&nbsp;<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 a "Error: 10 $digest() iterations reached. Aborting!" error, but I'm not sure why. Is it because pathElements() is returning a new array each time? Is there a way to get around this?

like image 765
alecbz Avatar asked Mar 19 '13 02:03

alecbz


2 Answers

Yes, this happens because you're returning a new array every time, and the $digest cycle loops infinitely (but Angular ceases it). You should 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 = 0 instead of creating a new one, to avoid it firing continuously.

You should also consider what @Flek suggests. Call this function only once, in the time you need it to recalculate. And all you binds should be directly over $scope.pathArray.

If you do need a function to test its clenaning state before using it, then at least I suggest you to create two separate functions, just to keep each function with it own attribution.

like image 141
Caio Cunha Avatar answered Nov 02 '22 13:11

Caio Cunha


For some nice reference on how to implement breadcrumbs in Angular check out the angular-app project (breadcrumbs service, how to use it).

Here's a demo plunker.

like image 45
Stewie Avatar answered Nov 02 '22 13:11

Stewie