javascript - Angular.js Directive controller inheritance -
assume have directive:
.directive('money', ['service', function (service) { /* * code */ controller: ['$scope', '$element', '$attrs', '$parse', function (scope, celement, attrs, $parse) { scope.somevalue=1; scope.somefunction = function(){ console.writeline('money'); } } and there second directive:
.directive('cash', ['service', function (service) { /* * code */ controller: ['$scope', '$element', '$attrs', '$parse', function (scope, celement, attrs, $parse) { scope.somevalue=1; scope.somefunction = function(){ console.writeline('cash'); } } as can see difference between 2 directives content of 1 function. perfect way inherit controller , shadow somefunction
is possible in angular make or should leave 2 directives small diferences?
why not have console directive grabs write attribute on directive's element?
so, instead of <div data-money></div> , <div data-cash></div> you'd have <div data-console text="money"></div> , <div data-console text="cash"></div>.
basically, pull what's different attributes can brought more generic directive.
based upon comments, how this? create controller standalone function, use same controller in both directives.
at point though, i'm not sure it's going save (or any) code , may overkill refactoring. considering minor differences, may make more sense keep way have it.
Comments
Post a Comment