3

Currently I have a factory method like this:

            open_model: function(som_id){
                var modelSettings = $aside({
                    controller: // a very big controller code
                    size: 'sm',
                    placement: 'right',
                    templateUrl: 'views/id/modal.html',
                    resolve: {
                        modal_data: function(){
                            return area_id;
                        }
                    }
                });
            },

I want to separate out the controller from this method and call it by name. Currently this is >100 lines of code in controller section and hence it makes factory dirty. So is there a way to define the controller somewhere else, and call the controller by name instead of writing the code.

undefined
  • 3,464
  • 11
  • 48
  • 90

3 Answers3

3

In addition to @nikjohn's answer, you can also create a separate angular controller and refer it within you modelSettings. Since you already said your controller code for the modal exceeds 100 lines, this even might be a better solution.

angular.module('myApp').controller('ModalInstanceCtrl', ['$scope', 'modal_data', function($scope, modal_data) {
    // modal controller code goes here
}]);

// and refer it in your factory method
open_model: function(som_id){
    var modelSettings = $aside({
        controller: 'ModalInstanceCtrl'
        size: 'sm',
        placement: 'right',
        templateUrl: 'views/id/modal.html',
        resolve: {
             modal_data: function(){
                  return area_id;
             }
        }
    });
}
Lizzy
  • 2,033
  • 3
  • 20
  • 33
  • hi @Lihini, your answer seems to be some solution for my problem. But can you also please assist me to pass some value to this controller as parameter (?possible). Something like this is possible? controller: 'ModalInstanceCtrl(parm1, param2)' – undefined Nov 30 '16 at 08:58
  • You've already done defining parameters using `resolve`. Just include it in the controller constructor. I've edited my answer to include `modal_data`. – Lizzy Nov 30 '16 at 10:27
  • Thanks @Lihini.. honestly speaking I was not aware of the purpose of resolve.. I just added that because in some of the previous code it was there. Now I understood it.. Thanks a lot.. – undefined Nov 30 '16 at 13:20
  • @Lihini Can you provide a example how to use `resolve` and catch those values form the `controller`? Is it something like `local` with `mdDialog` – Sanira Nimantha Oct 09 '18 at 13:07
  • @Sanira I've already added an example of how to use `resolve` here. Please check the `modal_data` field in `resolve` object and how it has been included in the controller. – Lizzy Oct 09 '18 at 13:12
  • @Lihini I have encountered an unexpected error with parameter injection from a factory. I have post a [question](https://stackoverflow.com/questions/52700765/passing-values-from-a-factory-to-controller-angularjs/52702649?noredirect=1#comment92367669_52702649) regarding that issue. I used a `mdDialog` and `local` to pass the parameters there, I tried it with the `resolve` also, but get the same error as before. If you could look into that question, that would be grate, any suggestions may be if you have faced this error before – Sanira Nimantha Oct 10 '18 at 04:38
1

You can define your controller wherever you want, and inject it as a dependency into your factory (although I don't know what a controller would be doing within a factory).

controllerFn: function(a, b) {
   // ......
}

open_model: function(som_id, controllerFn){
            var modelSettings = $aside({
                controller: controllerFn,
                size: 'sm',
                placement: 'right',
                templateUrl: 'views/id/modal.html',
                resolve: {
                    modal_data: function(){
                        return area_id;
                    }
                }
            });
        },
nikjohn
  • 20,026
  • 14
  • 50
  • 86
0

You should never access a controller within a factory. You're mixing concerns by doing that. If there's functionality you want to separate out, create another service and inject that service into your factory. Your controller should be leveraging services and factories, not the other way around.

Yatrix
  • 13,361
  • 16
  • 48
  • 78