zPrima
zPrima

Reputation: 737

angularjs - controller inheritance calling parent

I have a parent controller inside the first module and a "child" controller inside the second module. The second module has a dependancy to the first. I want my "child" controller to inherit the "parent" controller. But the problem is how to call the "parent" controller method.

For example:

 SecondModule.controller("childBrowseCtrl", function($scope, $injector, $controller){
        $injector.invoke(ParentBrowseCtrl, this, {$scope:$scope});

        //this overrides the onedit function from parent
        $scope.onEdit = function(){
            console.log("from edit console");

            //how do i make this work?
            ParentBrowseCtrl.$scope.onEdit();
        };

});

The html structure:

 <html>
   <head></head>

   <body>
      <div ng-view></div>
   </body>

   <script src="coreapp.js"></script>
   <script src="mainapp.js"></script>

   <script>
     angular.bootstrap(document,["MainApp"]);
   </script>

   </html>

Upvotes: 5

Views: 955

Answers (4)

Yasser Shaikh
Yasser Shaikh

Reputation: 47784

this worked for me

// in parent
$scope.parentFnCall = $scope.functionName;

// in child
$scope.parentFnCall()

Upvotes: 0

sp00m
sp00m

Reputation: 48827

This may work:

var parentOnEdit = $scope.onEdit;

$scope.onEdit = function() {
    parentOnEdit();
};

Upvotes: 4

Ruslan  Ismagilov
Ruslan Ismagilov

Reputation: 1348

You could try:

$scope.$parent.onEdit();

Great article about scopes: https://github.com/angular/angular.js/wiki/Understanding-Scopes

Upvotes: 0

Sten Muchow
Sten Muchow

Reputation: 6701

Controllers setup inheritence through the prototype chain so if your controller nesting structure in the html is correct then the child controllers will inherit prototypically.

You can check this if you

console.log($scope);

in the children controllers to see if they properly inherited from the parent.

Upvotes: 0

Related Questions