Angular.js: Is it possible to re-render ng-repeats based on existing scope data?

Jannis picture Jannis · Sep 26, 2012 · Viewed 16.2k times · Source

I have a bunch of connected ul lists in my view which are using a jQuery UI: Sortable directive to facilitate drag and drop and reordering of the list items.

Changes I make via jQuery UI's drag and drop I apply to the $scope using the $apply function, this part works…

The issue I am running into now however is that on drop into some of these lists I provide a custom form that the user needs to fill out.

The user has the option to:

  1. fill out the form and continue which then does the $apply call to persists the data into the $scope
  2. click a cancel button which instead of calling the $apply to store info, should revert the last drag/drop interaction effectively 're-rendering' all my lists to reflect the data that is still in the $scope at this stage (since the latest drag had not had any effect on it yet).

The effect of this "cancel" button is effectively reverting everything to the point before the user picked up the list item and dragged it into another list.

How can I force a 'refresh' or 're-render' of my ng-repeats so that they visually refresh and show the current $scope data again?

Answer

Andrew Joslin picture Andrew Joslin · Nov 5, 2012

When the user starts filling out the form, I would set

$scope.oldData = angular.copy($scope.data);

Then let the user edit $scope.data using the form as he likes.

Then if the user presses cancel, just set $scope.data = $scope.oldData.