1

I have a situation where i want to call a parent controller method from a directive on ng-change. The call to controller method never triggers, not sure what could be wrong. Any help would be much appreciated. Thanks

Sample Code:

<body ng-app="content-app" ng-controller="ParentController">
    <div header-menu report-manager="model"></div>
</body>

report-manager is a model.

Header-Menu Directive:

var $module = angular.module('content-app');

    $module.directive('headerMenu', function () {
        return {
            restrict: 'AE',
            scope: {
                reportManager: '='
            },
            templateUrl: '/scripts/app/directives/header-menu.template.htm'
        };
    });

header-menu.template.htm

<div>
  <ul>
     <li ng-repeat="filter in filters" ng-controller="ChildController" ng-change="method()" />
  </ul>
</div>

ParentController.

angular.module('content-app')
.controller("MainController", ['$scope', function($scope) {
    $scope.method = function () { // do something };
}]);

1 Answer 1

1

Method needs to be passed into directive with "&", you can pass functions with "&"

$module.directive('headerMenu', function () {
    return {
        restrict: 'AE',
        scope: {
            reportManager: '=',
            method: "&" // needs to be passed into the headerMenu directive
        },
        templateUrl: '/scripts/app/directives/header-menu.template.htm',
        link: function(scope, element, attrs){
            scope.makeChange = function(){
                scope.method();
            }
        }
    };
});

HTML:

<div header-menu method="method()" report-manager="model"></div> 

and

 <li ng-repeat="filter in filters" ng-controller="ChildController" ng-change="makeChange()" />
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.