Do you have to us ng-Controller in the view or can you just declare it in the $routeProvider?

后端 未结 3 1560
一整个雨季
一整个雨季 2021-01-14 12:19

I recently realized I had not used ng-Controller to declare the official scope of my controller in the HTML but had only mentioned it in $routeProvider and all APPEARS to be

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 13:03

    I would consider it correct, and an advantage the way you have done it, because it better relates the context of your view and bound controller by keeping things in the one place.

    Just bear in mind that providing your controller in the route definition is basically the same as putting an ng-controller="MyCtrl" inside the element containing your ng-view directive that matches your route view.

    Also, if you need more complex routing (eg: hierarchical views and controllers), then I would consider looking at ui-router to use instead of ngRoute

提交回复
热议问题