How to avoid a large number of dependencies in Angularjs

后端 未结 4 2075
旧时难觅i
旧时难觅i 2021-02-05 08:46

I have an Angular application. Its working good, but as my application is getting bigger I\'m worried about the large number of dependencies that I have to inject in each contro

4条回答
  •  萌比男神i
    2021-02-05 09:01

    I've been playing with the idea of bundling services based on controllers.

    So in your example you'd refactor your; AppFactory, Menu, filterFilter and Notice services into a single service e.g. ViewAppsServices.

    Then you'd use your services like ViewAppsServices.AppFactory.yourFunction().

    As I see it that way you can at least shift your injections into another file cleaning your controller up a bit.

    I think readability would suffer a bit since another developer would then have to look at bundles rather than the controller itself.

    Here's a JSFiddle I put together to demonstrate how it would work; this is how I'd imagine yours would work.

    .service('ViewAppsServices', ['AppFactory', 'Menu', 'filterFilter', 'Notice', 
    function (AppFactory, Menu, filterFilter, Notice) {
        return {
            AppFactory: AppFactory,
            Menu: Menu,
            filterFilter: filterFilter,
            Notice: Notice
        };
    } ])
    

提交回复
热议问题