2

I have a controller, and a factory that is using the controller's functions. The reason I do this, is because I want to use some functionalities in more controllers, depending on the actual $scope My solution would be something lik the code below. However, angular throws an error saying controllerFunction is undefined

EDIT: This code is working! I made a typo somewhere else in the code.

angular.module('myApp')
 .controller('myController', function ($scope, $http, myInterface) {
    var myFactory = new myInterface($scope);
    $scope.controllerFunction = function(){
        // do something 
    }
 })
 .factory('myInterface', function(){
    var self;
    function Interface($scope) {
      this.$scope = $scope;
      self = this;
    }

    Interface.prototype.interfaceFunction = function(){
      self.$scope.controllerFunction(); 
    }
    return Interface;
});
7
  • You can move controllerFunction() function to an specific service.. Than you can use it everywhere Commented Oct 22, 2016 at 17:08
  • are you trying to call $scope.controllerFunction from your factory? Commented Oct 22, 2016 at 17:09
  • @Ved Yes, I try to call it. Commented Oct 22, 2016 at 17:09
  • This is a simplified code, the actual code is a lot more complex. Basically I have to implement a function in the factory that calls a function from Controller1 and then call this factory-function in Controller2 Commented Oct 22, 2016 at 17:13
  • 1
    @DNagy Please check my answer and let me know if it works for you Commented Oct 22, 2016 at 17:18

2 Answers 2

2

You need to pass a callback method to your factory method from controller.

angular.module('myApp')
 .controller('myController', function ($scope, $http, myInterface) {
    myInterface.myMethod(function (){// callback method passed to factory
       $scope.controllerFunction();//will get called from factory via callback
   )}
    $scope.controllerFunction = function(){
        // do something 
    }
 })
 .factory('myInterface', function(){
     var myMethod = function (cb) {
        //your code
        cb();  //calling callback method of controller
     }

  return myMethod;

});
Sign up to request clarification or add additional context in comments.

Comments

2

You could do something like this, The problem with your code is you are passing the $scope, but after that you are defining the function. Note: $scope is an object and not a service which is singleton shared across. Each and every controller has its own $scope.

var myApp = angular.module("myApp", []);
myApp.controller('Ctrl', function($scope, NameService) {

    $scope.callController = function(){console.log("Called controller")};
    $scope.NameService = new NameService($scope);     
});

myApp.factory('NameService', function() {

    //constructor
    function NameService(scope) {
        this._scope = scope;
        this._someFunction()
    }

    //wherever you'd reference the scope
    NameService.prototype._someFunction = function() {
        this._scope.callController();
    }

    return NameService;

});

http://fiddle.jshell.net/5gmnvL6b/

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.