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 is dependent on the first. I want my "child" controller to inherit the "parent" controller. But the problem is how to call the "parent" controller method.

For instance:

 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();
        };

});

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
source share
4 answers

This may work:

var parentOnEdit = $scope.onEdit;

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

The controller establishes inheritance through the prototype chain, therefore, if the nesting structure of your controller in html is correct, child controllers inherit the prototype.

,

console.log($scope);

, , .

0

:

$scope.$parent.onEdit();

Great article on areas: https://github.com/angular/angular.js/wiki/Understanding-Scopes

0
source

it worked for me

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

// in child
$scope.parentFnCall()
0
source

Source: https://habr.com/ru/post/1537469/


All Articles