2016-08-24 70 views
0

我有一個父角控制器有一個方法,需要與其他控制器共享。父控制器看起來像這樣:從父控制器角度使用子控制器中的對象

"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); 

})(); 

我在另一個擴展此控制器有無限滾動在視圖中工作。這是孩子控制器:

"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); 

})(); 

在我看來,我有這樣的:

<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> 

我遇到的問題是,ctrl.assets總是空的,甚至認爲服務返回的數據。這是在父控制器中定義的ctrl.assets的問題,並且在子控制器中不可見?我如何使該資產對象共享到子控制器,以便我可以在用戶界面中看到數據?

回答

0

我最終做的是將一些控制器變量移動到$scope,現在看起來工作良好。 $scope正在被兒童共享