1
unified.controller('YourteamController', function uniYourteamController($scope) {    
    testController.listTeams();
});    

unified.controller('testController', function testController($scope) {
    $scope.listTeams = function() {
        //Listing process
    };    
});

I have two controller. I need to call a function(listTeams()) in another controller(testController). How to call this using angularjs

5

5 Answers 5

1

Go the service way. Easier to test, and more in line with 'best practices'.

angular.module('sharedService', function () {
  function listTeams () {
    return 'teams!';
  }

  angular.extend(this, {
    listTeams: listTeams
  })
});

angular.module('ctrl1', function ($scope, sharedService) {
  angular.extend($scope, {
    listTeams: sharedService.listTeams
  });
});

angular.module('ctrl2', function ($scope, sharedService) {
  angular.extend($scope, {
    listTeams: sharedService.listTeams
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

You need to inject first controller as dependency to second controller. Here is how to do it.

1 Comment

Whats the difference in your answer and my comment ?
0

do the following ...

  unified.controller('YourteamController', function uniYourteamController($scope) {    
        var testController= $scope.$new();
        $controller('testController',{$scope : testCtrl1ViewModel });
        testController.listTeams();
    });    

    unified.controller('testController', function($scope) {
        $scope.listTeams = function() {
            //Listing process
        };    
    });

Comments

0

you have to make a service that will return a function that can be used in both controllers having diffrent scope.

========================================

unified.controller('YourteamController', function uniYourteamController($scope, $abc) {

$abc.listTeams();
});

unified.controller('testController', function testController($scope, $abc) {
$abc.listTeams();
});

unified.service('abc', function () {


this.listitems = function () {

}
});

Comments

0

Just try this

unified.controller('YourteamController', function uniYourteamController($scope) {    
    $scope.$parent.listTeams();
}); 

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.