5

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>
4
  • How are you setting up the html structure? Commented Apr 22, 2014 at 8:08
  • edited my question to include html structure Commented Apr 22, 2014 at 8:14
  • Ok this doesnt show any controllers being nested. Are you nesting them with routing? Commented Apr 22, 2014 at 8:25
  • Nope, the only thing thats close to inheritance is that i invoke the parent controller inside the child. Commented Apr 22, 2014 at 8:42

4 Answers 4

4

This may work:

var parentOnEdit = $scope.onEdit;

$scope.onEdit = function() {
    parentOnEdit();
};
Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

$scope.$parent has the rootScope, there is no nesting of controllers in the html.
Controller do not inherit, but their $scope property does.
if i override the $scope.onEdit function, then the implementation of the parent onEdit is gone, that's whats bugging me.
@zPrima Then the structure is setup correctly - this is how prototypical inheritance works.
Somehow i did not try to save the onEdit function before overriding it :) @sp00m answer did just that.
|
0

You could try:

$scope.$parent.onEdit();

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

1 Comment

maybe onEdit() in more than one step-parent scope? $scope.$parent.$parent or more... but it is bad practice. If so, you need to rethink structure of this.
0

this worked for me

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

// in child
$scope.parentFnCall()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.