0

I have a Parent Angular Controller that has a method that needs to be shared with some other controller. The parent controller looks like this:

"use strict";

(function() {

var ParentCtrl = function($scope, atomico, Asset) {

var _this = this;

_this.busy            = atomico.identity == null;
_this.oldestTimestamp = null;

_this.assets = [];

/**
 * Infinite scrolling, fetches more assets when the user scrolls down.
 */
_this.fetch = function() {
  if (_this.noMoreAssets) { return; }

  _this.busy = true;

  Asset.all(atomico.metadata['campaign'].id, _this.oldestTimestamp, $scope.dates.start, $scope.dates.end, function(assets) {
    _this.busy = false;

    if (assets.length > 0) {
      _this.assets = _this.assets.concat(assets);

      _this.oldestTimestamp = moment(assets[assets.length - 1].start).unix();
    } else {
      _this.noMoreAssets = true;
    }
  });
};

 };

ParentCtrl.$inject = [ '$scope', 'atomico', 'Asset' ];

angular.module('myModule').controller('ParentCtrl', ParentCtrl);

})();

I am extending this controller in another one to have infinite scrolling to work in a view. This is the child controller:

"use strict";

(function() {

var ChildCtrl = function(atomico, userState, $controller, $scope) {

var _this = this;

angular.extend(_this, $controller('ParentCtrl', {$scope: $scope}));

// Fetch assets after user, campaign and account data is available.
atomico.ready(function(){
  var dates = userState.getCampaignViewData(atomico.metadata['campaign'].id).list_view;
  $scope.dates = _.isEmpty(dates) ? {start: moment(), end: moment()} : dates;
  _this.busy = false;
});
};

CampaignListCtrl.$inject = [ 'atomico', 'userState', '$controller', '$scope' ];

angular.module('myModule').controller('ChildCtrl', ChildCtrl);

})();

And in my view i have this:

<div id='agenda_viewer' ng-controller="ChildCtrl as ctrl">
    <p class="at-text-center at-block-center c-empty-list" ng-hide='ctrl.assets.length || ctrl.busy'>
        There are no assets to show for this day
    </p>
    <div class="agenda-flight__content at-row" infinite-scroll='ctrl.fetch()' infinite-scroll-disabled='ctrl.busy' infinite-scroll-parent="true">
        <div class="agenda-flight at-row agenda-asset__live" ng-repeat='asset in ctrl.assets' ng-init='asset.collapsed = false'>
            <directive-list-row asset='asset'></directive-list-row>
            <directive-list-expanded asset='asset' ng-if='asset.collapsed'></directive-list-expanded>
        </div>
    </div>
    <div class='c-loading' ng-show='ctrl.busy'>Loading data...</div>
</div>

The problem i am having is that the ctrl.assets is always empty even thought the service returns the data. Is this an issues with ctrl.assets being defined in the parent controller and not visible in the child controller? How can i make that assets object shared to the child controller so i can see the data in the UI?

Mohit Tanwani
  • 6,503
  • 1
  • 11
  • 31
Leon
  • 1,108
  • 2
  • 15
  • 37

1 Answers1

0

What I ended up doing is moving some of this controller variables into the $scope and now seems to be working good. $scope is being shared across children

Leon
  • 1,108
  • 2
  • 15
  • 37