javascript - Angular ngController vs Controller constructed within Directive -


i'm wondering use cases these 2 methods of creating controller:

using ngcontroller:

myapp.controller('mycontroller', ['$scope', function ( $scope ) {  }]); 

constructing controller within directive controller attribute:

myapp.directive ( 'mydirective', [ '$window', function( $window ) {     return {         restrict: 'a',         controller: [ '$scope', function( $scope ) {          }],         link: function( scope, element, attrs ) {          }     }; }]); 

is there reason wouldn't construct controller within directive if both invoked on same element?

is question of how used / complex controller is?

the reason use directive controller condensed in 1 sentence:

to create reusable components

directive controller should contain logic of component could reused. using directive controller isolate scope way create reusable components.

take paginator example: paginator needs logic notify other component (a grid example) of current selected page changed grid can update accordingly. these logic written inside directive controller to reused. when using isolate scope, scope not tight application controller'scope , it's easy configure pagesize bind property of application controller's scope.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -