toddmotto / angularjs-styleguide

AngularJS styleguide for teams
https://ultimateangular.com
5.96k stars 700 forks source link

Using require vs bindings in components #119

Closed alextsg closed 8 years ago

alextsg commented 8 years ago

I noticed that in the components section, it states that the require object is for referencing inherited logic alongside $onInit. What's the recommendation for using require versus putting a function in the component's bindings? For example, should I be doing:

require : {
   parent: '^parent'
}
...
$ctrl.parent.doStuff();

or

bindings: {
    doStuff: '&'
}
...
$ctrl.doStuff()

Thanks! Sorry if this is the wrong place for this!

toddmotto commented 8 years ago

Good question indeed! If you can, use bindings as much as possible and think about stateless/stateful components. Using require kind of goes against the principles of pure components, and inheriting controllers isn't as easy to test, or as reliable IMO (due to DOM placement etc).

alextsg commented 8 years ago

Yeah makes a lot of sense. Thank you!

toddmotto commented 8 years ago

Anytime :)!

Prasanna-Sridharan commented 5 years ago

Hi @alextsg @toddmotto I guess using 'require' makes sense when you want to utilize a directive API (like a service, e.g. custom logger API) which is not a parent to the component. Also require supports multiple API's to be referred. To access parent scope, 'bindings' are helpful and minimal. Please correct me if am wrong.