1

I am getting strange errors with minification, I suspect it has something to do with the fact i have a controller within the directive:

var app = angular.module('myapp');

app.directive("person", ['$dialog', function($dialog) {
return {
    restrict: "E",
    templateUrl: "person/views/person.html",
    replace: true,
    scope: {
        myPerson: '='
    },     
    controller: mycontroller
};

function mycontroller($scope, $attrs) {
  $scope.variable = 1;
}
}]);

What is the proper way to refactor this so it doesn't get broken during minifaction?

6
  • 4
    angular.module('myApp').controller('mycontroller', ['$scope', '$attrs',mycontroller]); Avoid using global controllers, register them using controller constructor function and just do the explicit dependency annotation just like what you are doing with the directive. Commented Dec 8, 2014 at 22:14
  • How does that controller end up where the directive is? Not seeing the relationship with how the mycontroller would end up there as currently the controller is declared inside the app.directive("person"). Commented Dec 8, 2014 at 22:16
  • 2
    I missed that part, however same thing applies there too. controller:'mycontroller' or just use inline array @ your controller key's value itself ex:- controller: ['$scope', '$attrs', mycontroller]. Commented Dec 8, 2014 at 22:17
  • How to use the inline? I've done it before but not within the directive's controller. Commented Dec 8, 2014 at 22:20
  • Like this jsbin.com/murujiqoji/5/edit Commented Dec 8, 2014 at 22:23

1 Answer 1

1

If you want to write it like this you have to use $inject

 mycontroller.$inject = ['$scope', '$attrs'];

Reference: https://github.com/johnpapa/angularjs-styleguide#style-y075

But I would sugest you do not use these kind of "mimimization friendly" dependency injection style and look on automatization tools which will do this for you - ng-annotate (or its gulp/grunt derivate which can be used in your build.)

Sign up to request clarification or add additional context in comments.

6 Comments

Is ng-annotate smart enough to find cases like the one I wrote above? The one on the site appears to be basic app.controller()
Yeah i forgot about ng-annotate.. @Rolando However if you really want to test the controller you could prefer registering them using controller constructor.
I would say its quite clever - From doc : github.com/olov/ng-annotate ng-annotate understands return {.., controller: function($scope) ..} inside a directive. and you can also add // @ngInject on the top of your function defininition, it will attach an $inject array to the function.
I have added reference to Reference: github.com/johnpapa/angularjs-styleguide#style-y075 where you can se how $inject should be used in your case
Yeah. though i have not used, i have heard ng-annotate can be used along with ng-doc header as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.