javascript - What's the 'Angular way' of a child updating a parent? -
there seems few ways of skinning cat, want know best (practice) , why.
if want collect data in child-cmp , pass parent-cmp, can:
- wrap object function parent , pass input child, calling function on input.
- make eventemitter function in child-cmp , emit result, collecting data in parent-cmp.
- access child through @viewchild() , directly call it's methods.
- create ngmodel (only if in form?) data collected
i'm sure there's more. feel using event emitter angulary thing do, non form based component. can't explain why technical foundation. thoughts?
@output()
first thing choose connecting child parent, when there no specific reasons other options.
creates least amount of coupling between components. if want use child in parent component, easiest way. angular components - reusability.a shared service option, when there no direct parent child relation.
*ngmodel
(implementingcontrolvalueaccessor
in child component) idea if component should used in forms.
avoid
acquiring reference of child using
@viewchild()
makes parent dependent on child component implementing specific interface.wrap object function (or otherwise passing method or object references)
passing methods around introduces strong coupling. use rather shared service. shared service class instance method, it's standard way in angular every developer used to, , it's more explicit. push updates participating components, use
Comments
Post a Comment