angularjs - Is it a good practice to have a separate definition for controller when using directive -
angularjs - Is it a good practice to have a separate definition for controller when using directive -
i new angular. , have been experimenting "directives". when using directives found next 2 ways utilize controller directive.
approach 1
angular.module('foldersettingapp') .directive('templaterenderer', function () { homecoming { // other options , controller: 'gridcontroller' }; }); approach 2
angular.module('foldersettingapp') .directive('templaterenderer', function () { homecoming { // other options , controller: function ($scope, $attrs) { // add together functions here } }; }); i not sure approach utilize when, suggestion appreciated.
it's not general utilize one, it's based on use,
for example, if want utilize directive multiple time in app, it's improve don't define controller in directive, this:
.directive('exampledirective', function (){ homecoming { restrict: "a", // or e or ae or c template: "<div>your template here</div>", link: function (scope, element, attributes) { // ever function want whould here: } } }); but generally, sake of angular's modularity, it's not thought define controller in directives.
because aim of directive utilize in multiple controller , multiple views.
so defining controller in directive, makes directive less modular restricted use.
angularjs
Comments
Post a Comment