I have a couple of nested directions. I am trying to be consistent and use controllerAs syntax. But I'm struggling to find a clean way for children to call parent methods that don't include a parent putting seemingly random functions in scope.
angular.module('app', [])
.directive('parent', function(){
return {
restrict: 'EA',
controller: 'ParentController',
controllerAs: 'parentCtrl',
}
})
.directive('child', function(){
return {
restrict: 'EA',
require: '^parent',
controller: 'ChildController',
controllerAs: 'childCtrl'
}
})
.controller('ParentController', function($scope){
var ctrl = this;
ctrl.type = "hot";
ctrl.nonInheritedFunction = nonInheritedFunction;
$scope.inheritedFunction = inheritedFunction;
function nonInheritedFunction(type){
ctrl.type = type;
if(ctrl.type == 'cold'){
}
}
function inheritedFunction(type){
ctrl.nonInheritedFunction(type);
}
})
.controller('ChildController', function($scope){
var ctrl = this;
ctrl.doAction = doAction;
function doAction(action){
if(action == 'flip_temperature'){
$scope.parentCtrl.nonInheritedFunction('hot');
$scope.inheritedFunction('hot');
}
}
})
source
share