title: Scope variable not updating in DOM after event listener changes it
body: <p>I have a variable defined in my $scope object and bound in the DOM.
  When the page loads, the value is displayed correctly.  I have a listener defined on the $window resize event which does fire appropriately when the window is resized.  I put a breakpoint in the function that gets called and it does have the correct value of picCols when run and it does change to the new value based on the current window width.  However after the function is finished the DOM does not update with the new value.</p>
  
  <p>Here is my controller.  The watched variable is $scope.picCols</p>
  
  <pre><code>.controller('AlbumEditCtrl', function($scope, $stateParams, $window) {
  $scope.album = {id: 1, date: '8/23/2014', title: 'My Album 1', photos:
      [
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'},
          {url: 'img/default-placeholder.png'}
      ]};
      $scope.photoRows = [];
      $scope.picCols = 0;
  
      $window.addEventListener("resize", function () {
          if(parseInt($window.innerWidth / 80, 10) != $scope.picCols)
          {
              $scope.recalculateImageColumns();
          }
      });
  
      $scope.recalculateImageColumns = function (){
          var photoRows = [];
          $scope.picCols = parseInt($window.innerWidth / 80, 10);
          console.log($scope.$id);
          var count = 0;
          var currentArray = -1;
          for(var i = 0; i &lt; $scope.album.photos.length; i++){
              if(count &lt; $scope.picCols){
                  if(count == 0){
                      count++;
                      currentArray++;
                      photoRows.push({photos:[$scope.album.photos[i]]});
                  }
                  else{
                      photoRows[currentArray].photos.push($scope.album.photos[i]);
                      if(count == $scope.picCols-1)
                          count = 0;
                      else
                          count++;
                  }
              }
          }
          angular.copy(photoRows, $scope.photoRows);
      };
      $scope.recalculateImageColumns();
  });
  </code></pre>
  
  <p>Here is the DOM</p>
  
  <pre><code>&lt;ion-view title="My BGCA"&gt;
  &lt;ion-content class="has-header padding"&gt;
      &lt;h2&gt;{{album.title}}&lt;/h2&gt;
      &lt;h5&gt;{{album.date}}&lt;/h5&gt;
      &lt;h5&gt;{{picCols}}&lt;/h5&gt;
      &lt;h5&gt;{{$id}}&lt;/h5&gt;
      &lt;div class="row" ng-repeat="photoRow in photoRows"&gt;
          &lt;div class="col" ng-repeat="photo in photoRow.photos"&gt;&lt;img ng-src="{{photo.url}}" width="75" height="75" /&gt;&lt;/div&gt;
      &lt;/div&gt;
   &lt;/ion-content&gt;
  &lt;/ion-view&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong>
  Here is the directive that ended up working nicely.</p>
  
  <pre><code>.directive(
  "ngResize",
  function($window) {
      function link( $scope, element, attributes ) {
          var domElement = element[0];
          function updateImageCapacity() {
  
                  var viewportWidth = domElement.clientWidth;
                  var capacity = parseInt(viewportWidth / 80, 10);
                  $scope.setImageCapacity( capacity );
          }
          updateImageCapacity();       
  
          window.angular.element($window).on('resize', function(){
              $scope.$apply( updateImageCapacity );
          });
  
          $scope.$on(
              "$destroy",
              function() {
                  window.angular.element($window).off('resize');
              }
          );
      }
  
      return({
          link: link,
          restrict: "A"
      });
  
  }
  )
  </code></pre>
  
  <p><a href="http://www.bennadel.com/blog/2476-my-approach-to-building-angularjs-directives-that-bind-to-javascript-events.htm?&amp;_=0.9274228068534285#comments_45548" rel="noreferrer">http://www.bennadel.com/blog/2476-my-approach-to-building-angularjs-directives-that-bind-to-javascript-events.htm?&amp;_=0.9274228068534285#comments_45548</a></p>