Angular ngController vs Controller constructed within Directive

前端 未结 4 1321
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 19:06

I\'m wondering what the use cases are for these two methods of creating a controller:

Using ngController:

myApp.controller(\'myContr         


        
4条回答
  •  情书的邮戳
    2021-01-04 19:57

    The reason to use directive controller is condensed in one sentence:

    To create reusable components

    Directive controller should contain logic of the component that could be reused. Using directive controller together with isolate scope is the way to create reusable components.

    Take a paginator as an example: a paginator needs some logic to notify other component (a grid for example) of the current selected page changed so that the grid can update accordingly. These logic could be written inside directive controller to be reused. When using together with isolate scope, this scope is not tight to application controller'scope and it's easy for you to configure pageSize to bind to any property of the application controller's scope.

提交回复
热议问题