NiseNise
NiseNise

Reputation: 1162

Is this correct way dependency injection in Angular?

So I am new to Angular, I have been reading around the subject of dependency injection.

I added this function into my app but keep getting a eslint error :

You should use the function syntax for DI

I can not see anything wrong with what I have declared. Can anyone help?

angular.module('tools').config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { 
        $routeProvider.otherwise({ redirectTo : '/404' });
        $locationProvider.html5Mode(true)
    }
]);

the exact message I get is " 1:1 error You should use the function syntax for DI angular/di"

Upvotes: 2

Views: 3023

Answers (2)

Nebulosar
Nebulosar

Reputation: 1855

Instead of disabling the linting, you could also actually move it to a function.

If you are using something like ngInject, you could do:

angular.module('tools').config(myRoutConfig);


/** @ngInject */
function myRoutConfig($routeProvider, $locationProvider) { 
    $routeProvider.otherwise({ redirectTo : '/404' });
    $locationProvider.html5Mode(true)
}

Hope this helps someone!

Upvotes: 0

Ionut Costica
Ionut Costica

Reputation: 1392

It has to do with the default configuration of eslint-plugin-angular. Just put in

/*eslint angular/di: [2,"array"]*/

at the beginning of your js file and see if the error disappears. If so, add

"angular/di": [2, "function"]

to your eslint config

Note: eslint requires you to use consistent dependency injection, so you'll always have to use the same style of dependency injection you choose for your project

Source: eslint-plugin-angular docs

Upvotes: 4

Related Questions