Is it good to have main controller in Angular?

前端 未结 2 1085
暗喜
暗喜 2021-02-05 08:20

I dont know if this is a good practice... I have a controller defined in route config but because my HomeCtrl is in ng-if statement he cannot listen fo

2条回答
  •  Happy的楠姐
    2021-02-05 08:47

    I start all of my Angular projects with:

    The use of a "global" controller may not be necessary, but it is always nice to have it around when a need arises. For example, I use it in my CMS to set a binding that initiates the loading of everything else - so all the sub controllers are loaded because of it. That isn't violating separation of concerns because the global controller's concern IS to facilitate the loading of other controllers.

    That said, just be sure to keep things as modular/separated and reusable as possible. If your controllers rely on the global controller's existence in order to function, then there is an issue.

提交回复
热议问题