title: AngularJS : Directive : Animate sliding pages and postpone controller binding
body: <p>I'm trying to figure out how to postpone the default controller binding and only apply it from within the directive, once the specific animation (custom - not the angularjs one) has been performed on the DOM element with the content. The fiddle to what I'm trying to accomplish is here, although it does replace the content right away - before the sliding animation has completed: <a href="http://jsfiddle.net/scabro/hHy7s/27/" rel="nofollow">http://jsfiddle.net/scabro/hHy7s/27/</a></p>
  
  <p>I need to be able to postpone the $scope binding until the page container has completed sliding / animated up, then replace the content with the data from within the controller and once this has been done, slide back down.</p>
  
  <p>Here's what I currently have:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div id="wrapper" ng-app="myApp"&gt;
  
      &lt;div page-animate&gt;
  
          &lt;p&gt;
              &lt;a class="btn btn-primary" href="#/" ng-click="slidePage()"&gt;Home&lt;/a&gt;
              &lt;a class="btn btn-info" href="#/users" ng-click="slidePage()"&gt;Users&lt;/a&gt;
              &lt;a class="btn btn-success" href="#/pages" ng-click="slidePage()"&gt;Pages&lt;/a&gt;
          &lt;/p&gt;
          &lt;div id="relativeContainer"&gt;
  
              &lt;div id="content" ng-view=""&gt;&lt;/div&gt;
  
          &lt;/div&gt;
  
      &lt;/div&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>CSS:</p>
  
  <pre><code>#wrapper {
      text-align:center;
      padding:30px 0;
  }
  #relativeContainer {
      text-align:left;
      position: relative;
      width: 500px;
      height: 800px;
      min-height: 800px;
      margin: 0 auto;
      overflow: hidden;
  }
  #content {
      position: absolute;
      top: 0;
      left: 0;
  }
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>var myApp = angular.module('myApp', []);
  
  myApp.directive('pageAnimate', function() {
  
      return {
  
          restrict: 'A',
  
          scope: { },
  
          link: function (scope, elem, attrs) {
  
              scope.slidePage = function() {
  
                  var thisContainer = $('#content');
  
                  var thisHeight = thisContainer.outerHeight();
  
                  thisContainer.animate({ top : '-' + thisHeight + 'px'}, { duration : 300, complete : function() {
  
                      thisContainer.css({ top : '-999999em' });
  
                      var thisNewHeight = thisContainer.outerHeight();
  
                      $('#relativeContainer').animate({ height : thisNewHeight + 'px' }, { duration : 200, complete : function() {
  
                          thisContainer.css({ top : '-' + thisNewHeight + 'px' }).animate({ top : 0 }, { duration : 300 });
  
                      }});
  
                  }});
  
              };
  
          }
  
      }
  
  });
  
  
  myApp.config(function($routeProvider) {
  
      $routeProvider
          .when('/',
              {
                  controller: 'HomeController',
                  template: '&lt;h1&gt;{{ heading }}&lt;/h1&gt;&lt;p&gt;{{ content }}&lt;/p&gt;'
              }
          )
          .when('/users',
              {
                  controller: 'UserController',
                  template: '&lt;h1&gt;{{ heading }}&lt;/h1&gt;&lt;p&gt;{{ content }}&lt;/p&gt;'
              }
          )
          .when('/pages',
              {
                  controller: 'PageController',
                  template: '&lt;h1&gt;{{ heading }}&lt;/h1&gt;&lt;p&gt;{{ content }}&lt;/p&gt;'
              }
          )
          .otherwise(
              {
                  redirectTo: '/'
              }
          )
  
  });
  
  myApp.controller('HomeController', function($scope) {
  
      $scope.heading = 'Home page';
      $scope.content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate';
  
  });
  
  myApp.controller('UserController', function($scope) {
  
      $scope.heading = 'Users';
      $scope.content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.';
  
  });
  
  myApp.controller('PageController', function($scope) {
  
      $scope.heading = 'Pages';
      $scope.content = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.';
  
  });
  </code></pre>