1

i would like to reuse the code defined between the controllers

.controller('GenericController', ['$scope', '$controller', '$rootScope', '$dialogs', '$state', '$http', '$modal', '$q', '$timeout', 'projectFactory', 'projectPromise', 'phaseFactory', 'buFactory', 'stakeholderGroupFactory', 'ldapFactory', 'genericFactory', 'User',

    function ($scope, $controller, $rootScope, $dialogs, $state, $http, $modal, $q, $timeout, projectFactory, projectPromise, phaseFactory, buFactory, stakeholderGroupFactory, ldapFactory, genericFactory, User) {

      $scope.testing = function() {
        console.log("Hello");
      };
}]);
4
  • You can use a service to achieve this. Commented Jul 20, 2017 at 6:43
  • i am using a service. but i would like to intitiate the inheritace of code in side controllers Commented Jul 20, 2017 at 6:44
  • Use as little code as possible that still produces the same problem Commented Jul 20, 2017 at 6:46
  • If you are using a service, then post the code for the same and explain properly where you are facing the issue Commented Jul 20, 2017 at 6:48

2 Answers 2

3

You can use the factory and create the object for the function to reuse it.

app.factory("sample",function(){
    return function() {
        console.log("Hello");
      };
})

else collating multiple common functions

app.factory("commonFunctions",function(){

    commonFunction1(){
       console.log("common func1")
    } 

    commonFunction2(){
       console.log("common func2")
    } 

   return {
         commonFunction1: commonFunction1,
         commonFunction1: commonFunction2
   };
})
Sign up to request clarification or add additional context in comments.

2 Comments

Will factory act a a mirror for the controller communication
If you want to communicate between the controllers you can use service or localstorage or broadcast or cache, those will store the date and you can use it in other controllers. To reuse the functions you can create a factory, you can define what to be returned in factory.
0

Used the $controller for the importing this fixed my issue

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.