mp1990
mp1990

Reputation: 10249

Angular $controller service and minification

I am defining a new controller like this:

$controller(function ($scope) {}, {})

But when i turn on strict mode, this throws me an error saying $scope have to be declared as an annotation.

The problem is, is that the $controller service accepts only a function method or a controller name, and not an array where i can define my annotations.

https://docs.angularjs.org/api/ngMock/service/$controller

Does anyone have an idea how i can get around this? The reason that it has to work in strict mode, is because all my code gets minified, so if it doesnt work in strict mode, it doesnt work either when its minified.

Thank you in advance,

Mathias

Upvotes: 0

Views: 84

Answers (3)

Follow any of these approaches for adding DI in angularjs.
1)

mycontroller.$inject = ['$scope'];
function mycontroller($scope){}

2)
angular .module('app',[]) .controller('mycontroller',['$scope',function(){ // write controller code here}]);

3) Using ngAnnotate.

Upvotes: 1

Alexey Katayev
Alexey Katayev

Reputation: 248

You can try this (i didn't check, sorry):

myNamedFunction.$inject = ['$scope'];
function myNamedFunction($scope) {

}

$controller(myNamedFunction, {});

Upvotes: 1

jkordas
jkordas

Reputation: 155

You can name your function and use $inject.

$controller(myController, {});

myController.$inject = ['$scope'];
function myController($scope) {}

Upvotes: 1

Related Questions