Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am a beginner in angular..I have a requirement to avoid hard coding any names in the app.. I have used

myapp.constant("constantName",
{
    "name":"name1"
});

to avoid hard coding html..I would also like to avoid hard coding controller names also...

example :

myapp.controller("IndexCtrl",function($scope){});

Instead of having the string IndexCtrl in the controller, is there any way to define something like this

myapp.controller(constantName.controller,function($scope){})

where the constant will be

myapp.constant("constantName",
{
    "name":"name1",
    "controller" : "IndexCtrl"
});

Thanks for the help :)

share|improve this question
2  
Your requirement seems possibly misguided and maybe ridiculous. –  mccainz Apr 15 at 23:32
    
How will you manage injections with this scheme? You will need "named" constants, so um, how is that better? –  aet Apr 15 at 23:39
add comment

1 Answer

I have not seen anything quite like that before, but what I have seen (and done) is define your controller like so:

(function(){
    var controllerName = 'MyCtrl';
    angular.module('myModule').controller(controllerName, function($scope, $log){
        // Now in my controller I have full access the controller name 
        // this can be used for any logging that I may need to do
        $log.debug('something broke in ' + controllerName);
    });
})();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.