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'm using ant to build/uglyfy my AngularJS and each time I have a controller in a directive, I get an undefined $scope error. Example of code is this:

app.directive('directive1',['$compile', '$http',function($compile, $http) {
    return {
        restrict: "A",
        controller : function($scope) {
            var posts = {};
            this.addPosts = function($scope, newPosts) {

                angular.extend(posts, newPosts);
                //$scope.$apply(function() {
                    $scope.posts = posts;
                //});
            };
        },
        link: function (scope, element, attrs) {
           scope.posts = {};
        }
    };
}]);

My question is, how can I define the $scope of the controller to when it is compiled it doesn't come up as undefined?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You want to apply the same minifier/ulgifier safe approach you did on the directive definition to your controller.

So your controller goes from:

controller : function($scope) {

to:

controller: ['$scope',function($scope) {

The uglifier is renaming $scope to something short like 'e' which is great for size but Angular doesn't know what to inject into 'e'.

The uglifier won't change a string. So the new code, above, lets angular know it should inject $scope into 'e' (or whatever name the uglifier gives it).

share|improve this answer
    
You shouldn't need to, directive controllers have a set number of args. They are not freely injectable as a real controller is. –  Jonathan Rowny Nov 26 '13 at 22:45
1  
Actually, I'm wrong. This has changed... what I said used to be true but now angular actually does support injection and bracket notation. –  Jonathan Rowny Nov 26 '13 at 22:51

this.addPosts = function($scope, newPosts) {

What is calling this function? Why are you trying to pass $scope into it? I think you should change it to:

$scope.addPosts = function(newPosts) {

Because you want to get the first $scope... not whatever scope you're attempting to pass into addPosts.

share|improve this answer
    
Not the problem or concern, the problem is the $scope does not persist when uglyfied. It works otherwise. –  Devin Dixon Nov 26 '13 at 22:39
    
It might be your problem. A minifier will rewrite the first arg of controller as something like function(a) then the addPosts function will get minified as function(b, c)... I can't tell why you're writing your addPosts function like that. Can you explain why it's passing $scope into addPosts? That's just... odd. –  Jonathan Rowny Nov 26 '13 at 22:43

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.