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.
angular.module('mainApp').
  controller('dynamicRouteController', ['$scope', '$controller', '$routeParams', function($scope, $controller, $routeParams) {
    if(/^\d+$/.test($routeParams.pageOrName)) {
      $scope.controller = $controller('thisController', { $scope: $scope }).constructor;
      $scope.templateUrl = '/www/thisPage';
    } else {
      $scope.controller = $controller('thatController', { $scope: $scope }).constructor;
      $scope.templateUrl = '/www/thatPage';
    }
  }]);

this minifies to:

"use strict";
angular.module("mainApp").
controller("dynamicRouteController",["$scope",‌​"$controller","$routeParams",function(a,b,c){
/^\d+$/.test(c.pageOrName)?
(a.contro‌​ller=b("thisController",{$scope:a}).constructor,a.templateUrl="/www/thisPage"):
(a‌​.controller=b("thatController",{$scope:a}).constructor,a.templateUrl="/www/thatPa‌​ge")
}])

This is having issues minifying, I think its because of the {$scope : $scope} being altered... First time I have run into this/used this method. Anyone know a better way to write this so it minifies correctly?

EDIT: so whats happening, is that it is passing the {$scope: a} which is fine, but on that referenced controller, when its minified, that $scope has become a or b or e depending... so if I write the code "pre-minified", meaning I literally find what letter represents $scope in the other controller, I can get it to work, but thats so hacky! Again, any ideas?

Using Grunt for minification Angular 1.0.5 ... maybe fixed in later versions?

2nd EDIT: A decent answer is to throw both controllers into the same file, explicitly... which is ugly... but it works! so with in one controller, I am declaring 2 sub controllers, which is lame. If you know of another way please share with the class!

share|improve this question
1  
Just taking a wild guess here but {"$scope": $scope}? –  Phil Aug 8 '13 at 23:20
1  
There's a comment on the $controller doco that has {$scope: {}}. Give that a try –  Phil Aug 8 '13 at 23:23
1  
Just another wild guess var scope = $scope; then $controller('thatController', { $scope: scope }).constructor; Similar to how one would achieve the same end when testing a controller. Although when testing you tend to use var scope = $rootScope.$new(); –  Jonathan Palumbo Aug 8 '13 at 23:39
2  
Could you provided the code after it is minified? Perhaps that would provide some insight? –  Jonathan Palumbo Aug 9 '13 at 0:03
1  
What tool are you using for minification? –  Ye Liu Aug 9 '13 at 2:28
show 5 more comments

1 Answer

up vote 2 down vote accepted

I was working on this issue with @mclenithan and what we came up with is:

$scope.controller = ['$scope', 'service1', 'service2', 
    $controller('thisController', { $scope: $scope }).constructor];

The main issue was the controllers thisController and thatController had more parameters to inject than just $scope (in this example it expects service1 and service2).

The $controller(...).constructor was returning the minified controller function with the parameters renamed to a, b, c, d, etc. When Angular was trying to instantiate the controller it had issues trying to find the correct services to inject.

Using the array notation instead of just the controller function fixed the issue. See a note on minification in the tutorial for more info.

Also see this question for context on what we were trying to do to begin with.

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.