how can i run the function in 2 separate controllers in angularjs

advertisements

i have 2 separate angularjs controller

that named HomeController And SearchController

i have a function that named "Search()" in HomeController

how can i run search function from searchController ?


define the 'search' function in a factory, inject that factory into both controllers then you can access 'search' function from both controllers.

sample:

app.controller('HomeController', function(searchFactory){
   //calling factory function
   //searchFactory.search();

});

app.controller('searchController ', function(searchFactory){

   //calling factory function
   //searchFactory.search();

});

app.factory('searchFacotry', function(){
  return{
    search: function(arg){
      alert('hello world');
    };
  };
});