Angular JS - How can i animate on model change?

前端 未结 2 1045
没有蜡笔的小新
没有蜡笔的小新 2021-02-12 14:30

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 foll

2条回答
  •  梦如初夏
    2021-02-12 14:48

    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:

    Pricing Poll

    {{currentVertical.title}}

提交回复
热议问题