AngularJS: Why do custom directives default to being attribute-only? -
egghead.io has great explanation of directive restrictions in angularjs. custom directive can defined follows:
angular.module("app", []).directive("blah", function () { return { restrict: "a", template: "blah directive. nothing see here." }; });
this creates call attribute directive (due restrict: "a"
) purposes of asking question. angular's default restriction on custom directive, , directive can used like:
<div blah> <!-- content of directive --> </div>
when want create custom directive, however, go element directive, like:
<blah> <!-- content of directive --> </blah>
how former attribute directive better latter element directive , why chosen default?
this opinion, only:
there 3 possible ways define directive in html - attributes, elements , classes. classes lax , confusing, , best practice separate logic style. element directives strict - can have 1 "element" directive in dom element. makes them special right start.
an attribute seems best middle ground between these 2 extremes - clear, allows multiple directives in element (if following egghead's videos, superhero example on "directive directive communication" shows kind of "directive hierarchy", elements superseding attributes. also, , important of times (i program intranet apps, me isn't) attributes allow angularjs templates valid html.
edit - 2 cents be doesn't matter - in real scenario, bad idea trust "default" configuration of primary restrict option - setting explicitly makes clear, no doubt directives (especially working in team projects, anytime, really)
Comments
Post a Comment