dreamer
dreamer

Reputation: 901

What is the difference between a controller of a directive and a controller of an app

What is the difference in functionality between a controller of a directive and a controller of myApp module defined in below snippet?

How exactly are we supposed to use directive's controller and a module's controller, so that we make maximum utilization of the framework.

In case of differences, 1 or 2 examples showing the difference would really help a lot of newbies around.

JS snippet

angular.module('myApp',[])
.controller('trialCtrl',function($scope){})
.directive('trial',function(){
  return{
    restrict:'CEAM'
    scope:{},
    link:function(scope,elem,attr){},
    controller:function(){},
    template:""
 }
})

Upvotes: 0

Views: 85

Answers (1)

antoinestv
antoinestv

Reputation: 3306

There is no difference, you could replace this "directive controller" with a string representing another controller.

Example:

angular.module('myApp',[])
.controller('trialCtrl',function($scope){})
.controller('myController',function($scope){})
.directive('trial',function(){
  return{
    // ...
    controller: 'myController'
    // ...
 }
})

Note: That's even cleaner to do it.

Upvotes: 2

Related Questions