Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS - How can i animate on model change?

i'm trying to do a nice fadeout+fadein transition when the currentVertical changes. in knockout it was so simple but i can't figure it out here. please help.

the following code displays a UL list which is "bound" to a pricings array in the $scope.currentVertical when an LI element is clicked, the $scope.currentVertical is changed and the UL list updates accordingly. This works fine, but i would like the entire #container div to fade out and fadein when $scope.currentVertical is changed. Please help...

My html:


<body>
    <h1>Pricing Poll</h1>
    <div ng-controller="VerticalsController">
        <div id="container">
            <h2>{{currentVertical.title}}</h2>

            <ul>
                <li ng-repeat="pricing in currentVertical.pricings">
                    <a ng-click="currentVertical.selectPricing(pricing)">{{pricing.name}}</a>
                </li>
            </ul>
        </div>
    </div>
</body>

my javascript:

function VerticalsController($scope) {

  $scope.verticals = [
    {
        title:'internet',
        pricings: [
            {
                name: 'netvision',
                monthly: 23
            },
            {
                name: 'hot',
                monthly: 33
            },
            {
                name: '012',
                monthly: 28
            }
        ]
    },
    {
        title:'cellular', 
        pricings: [
            {
                name: 'cellcom',
                monthly: 20
            },
            {
                name: 'pelephone',
                monthly: 30
            },
            {
                name: 'orange',
                monthly: 25
            }
        ]
    },
    {
        title:'banks', 
        pricings: [
            {
                name: 'leumi',
                monthly: 20
            },
            {
                name: 'poalim',
                monthly: 30
            },
            {
                name: 'benleumi',
                monthly: 25
            }
        ]
    }];

    $scope.selected = [
    ];

    $scope.currentIndex = 0;
    $scope.currentVertical = $scope.verticals[0];

  $scope.selectPricing = function(pricing) {
    $scope.selected.push(pricing);
    $scope.currentIndex++;
    $scope.currentVertical = $scope.verticals[$scope.currentIndex];
  };

  /*$scope.remaining = function() {
    var count = 0;
    angular.forEach($scope.todos, function(todo) {
      count += todo.done ? 0 : 1;
    });
    return count;
  };*/
}
like image 905
Uri Abramson Avatar asked Feb 08 '13 15:02

Uri Abramson


People also ask

Which animation strategy would you use to apply multiple styles for a transition?

The keyframes() function in Angular allows you to specify multiple interim styles within a single transition, with an optional offset to define the point in the animation where each style change should occur.

How do you add animations to Main ng model in angular 4?

We have to import the animation function that is to be used in the . ts file as shown above. import { trigger, state, style, transition, animate } from '@angular/animations'; Here we have imported trigger, state, style, transition, and animate from @angular/animations.

What is angular animate JS?

The ngAnimate module does not animate your HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, the element gets some pre-defined classes which can be used to make animations. The directives in AngularJS who add/remove classes are: ng-show. ng-hide.


2 Answers

You have to use custom or create directives to start advanced DOM manipulation like animations.

Here's a fiddle with the animation you requested, I use the visible variable on scope to trigger fading and the $timeout service to only change the selectedItem when fadeOut, it could be improved to pass a timeout and a callback as a directive option...

Fiddle: http://jsfiddle.net/g/Bs66R/1/

JS:

function VerticalsController($scope, $timeout) {

  $scope.verticals = [{
    title:'internet',
    pricings: [{
      name: 'netvision',
      monthly: 23
    },
    {
      name: 'hot',
      monthly: 33
    },
    {
      name: '012',
      monthly: 28
    }]
  },
  {
    title:'cellular',
    pricings: [{
      name: 'cellcom',
      monthly: 20
    },
    {
      name: 'pelephone',
      monthly: 30
    },
    {
      name: 'orange',
      monthly: 25
    }]
  },
  {
    title:'banks',
    pricings: [{
      name: 'leumi',
      monthly: 20
    },
    {
      name: 'poalim',
      monthly: 30
    },
    {
      name: 'benleumi',
      monthly: 25
    }]
  }];

  $scope.selected = [
  ];

  $scope.currentIndex = 0;
  $scope.currentVertical = $scope.verticals[0];

  $scope.selectPricing = function(pricing) {
    $scope.selected.push(pricing);
    $scope.currentIndex++;
    $scope.visible = false;

    $timeout(function(){
      $scope.currentVertical = $scope.verticals[$scope.currentIndex];
      $scope.visible = true;
    }, 1000);
  };

  $scope.visible = true;
}

var fadeToggleDirective = function() {
  return {
    link: function(scope, element, attrs) {
      scope.$watch(attrs.uiFadeToggle, function(val, oldVal) {
        if(val === oldVal) return; // Skip inital call
        // console.log('change');
        element[val ? 'fadeIn' : 'fadeOut'](1000);
      });
    }
  }
}

angular.module('app', []).controller('VerticalsController', VerticalsController).directive('uiFadeToggle', fadeToggleDirective);

angular.bootstrap(document.body, ['app']);    angular.bootstrap(document.body, ['app']);

HTML:

<h1>Pricing Poll</h1>
<div ng-controller="VerticalsController">
  <div id="container" ui-fade-toggle="visible">
    <h2>{{currentVertical.title}}</h2>

    <ul>
      <li ng-repeat="pricing in currentVertical.pricings">
        <a ng-click="selectPricing(pricing)">{{pricing.name}}</a>
      </li>
    </ul>
  </div>
</div>
like image 179
Guillaume86 Avatar answered Oct 20 '22 13:10

Guillaume86


I recommend you use the new ngAnimate directive provided in the AngularJS Core.

Read more here

like image 34
Kenneth Lynne Avatar answered Oct 20 '22 13:10

Kenneth Lynne