AngularJs call parent controller function with parameter

14,275

The mistake in your code is with this line:

//This assigns the RESULT of parentTest() to $scope.test
$scope.test= $scope.$parent.parentTest(t);

// Either of the 2 options below will probably work for you.

//This assigns the parentTest Function itself to $scope.test
$scope.test= $scope.$parent.parentTest;

//This wraps it safely in case the parentTest function isn't ready when
// this controller initializes
$scope.test= function(t){$scope.$parent.parentTest(t)}; // << Change it to this!
Share:
14,275

Related videos on Youtube

user4773604
Author by

user4773604

Updated on June 04, 2022

Comments

  • user4773604
    user4773604 almost 2 years

    I have two controller's and I want to call other parent controller function with parameter when user clicks the button.

    Html

    <a ng-click='test('something')'>Click</a>
    

    Controller

    controller: function($scope) {
            ..........
       $scope.test= $scope.$parent.parentTest(t);// it fails...
    
    ..........}
    

    Parent Controller

    $scope.parentTest= function(m) {
    
       var my=m;
    
       ...Something...
    
    }
    

    If I run function without any parameter, it works. If I run the function with parameter it doesn't.

    I want to call parent function with parameter.

    • DaImTo
      DaImTo about 9 years
      Formatting and grammar.
  • HankScorpio
    HankScorpio about 9 years
    Can you elaborate on that? Console error? What happens? Is $scope.$parent.parentTest undefined? Is $scope.test called at all?