3

I'm trying to animate a div with some content whenever the $scope variable is updated. I want to give the user feedback that an update has occurred.

In my controller I update the $scope through a websocket like this:

socket.syncUpdates('item', $scope.items, function(event, item, array) {
    $scope.items = array;
});

I can't get my head around how to do this. I'm using ng-animate and animation.css

1 Answer 1

3
socket.syncUpdates('item', $scope.items, function(event, item, array) {
    $scope.items = array;
    $scope.$apply();
});

In your syncUpdates, trigger $digest cycle using $apply().

Now you can do animation in two places. Either do it inside this syncUpdates itself below the $scope.$apply() else in a separate $watch.

$scope.$watch("items", function(newValue, oldValue) {
     // your animation code here
}, true);

To animate the div, Here is one sample:

myApp.animation(".fade", function () {
  return {
    addClass: function (element, className) {
      TweenMax.to(element, 1, {opacity: 0});
    },    
    removeClass: function (element, className) {
      TweenMax.to(element, 1, {opacity: 1});
    }
  }
})

In your controller:

Inject $animate inside your controller function. Then use

    $animate.addClass(document.querySelector("selector"), "fade");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I'm going to try it out, do you have an example with using animate.css? Also this doesn't use the ng-animate at all does it?
check this one: thinkster.io/egghead/animating-the-angular-way it will be useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.