Self explanatory fiddle: http://jsfiddle.net/5FG2n/1/

Say I have a view with two controllers, one containing the other. The outer 
controller is static, but I need to set the inner controller based on a 
scope variable from the outer. The scope variable will be the inner 
controller's name as a string (eg. 'InnerCtrl').

Here's the view:

<div ng-app='app' ng-controller='OuterCtrl'>

    <div ng-controller='dynamicCtrl'>

        {{result}}

    </div>

</div>

And here's the module that is not yet correct:

angular.module('app', [])

    .controller('OuterCtrl', ['$scope', 

        function($scope) {

            // Instead of hard coding the controller here,

            // how do I resolve the string 'InnerCtrl' to the 

            // controller function defined below, so it can be

            // assigned to this dynamicCtrl property?

            $scope.dynamicCtrl = function($scope) {

                $scope.result = 'not working yet';

            };

            //eg:

            //$scope.dynamicCtrl = resolveCtrl('InnerCtrl');                
                               

        }

    ])

    .controller('InnerCtrl', ['$scope', 'service',

        function($scope, service) {

            $scope.result = service.getMessage();

        }

    ])

   .factory('service', function() {

       return {

           getMessage:  function() { return 'working!'; }

       };

    });

I tried using the $injector service:

$scope.dynamicCtrl = $injector.get('InnerCtrl');
But this gives: "Unknown provider: InnerCtrlProvider <- InnerCtrl"

I also thought about using the $controller service:

$scope.dynamicCtrl = $controller('InnerCtrl', {} /* don't want to hard code 
dependencies */);

But this gives me "Unknown provider: $scopeProvider <- $scope". I know I 
could create a scope to pass in, but I feel that angular should be 
automatically resolving the scope and service dependencies for me.

*How do I resolve a controller function from its name as a string, together 
with its dependencies, so it can be assigned to a scope variable?*

Originall asked on stackoverflow 
here<http://stackoverflow.com/questions/21179882/how-do-i-resolve-and-assign-an-inner-controller-from-the-outers-scope>
.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to