javascript - When is it appropriate to use a directive scope without its own controller? -
my understanding directives should concerned dom interaction , templating. setting application logic related $scope
responsibility of controllers instead.
yet angular allows create scope directive itself, such { scope: true }
. if this, when supposed initialize in controller constructor -- in post-link function? seems inappropriate use of directive, since it's not concerned dom.
when it appropriate use angular directive scope instead of creating controller must used directive instead?
yet angular allows create scope directive itself, such { scope: true }. if this, when supposed initialize in controller constructor -- in post-link function? seems inappropriate use of directive, since it's not concerned dom.
a directive can have own controller, defined controller
option. controller has same function application controller: provide behavior directive. said, think correct place initialize scope directive's controller, not link function.
i think of directive's controller place manipulate scope , of directive's link function place manipulate dom. so question has insights on matter.
when appropriate use angular directive scope instead of creating controller must used directive instead?
off top of head can think of 2 scenarios:
- you want directive access parent scope, don't want modify parent scope's data. in order need set directive have own scope, i.e.
scope: true
. - you want directive reusable. in such case, directive shouldn't rely on parent scope, , should have own isolate scope, i.e,
scope: {}
.
i've implemented directive , used controller function driving behavior , link function manipulating dom. perhaps you'd interested in checking out. link here.
Comments
Post a Comment