Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

1.5 Component() syntax and Directive required controllers #13746

Closed
ev45ive opened this issue Jan 12, 2016 · 4 comments
Closed

1.5 Component() syntax and Directive required controllers #13746

ev45ive opened this issue Jan 12, 2016 · 4 comments

Comments

@ev45ive
Copy link

ev45ive commented Jan 12, 2016

Hello,
I started using module.component() syntax for my directives and it works well, its clean and saves a lot of typing.

The only issue i have is that i used a lot the directives with "require" for inter-directive communication.
Is there a roadmap for adding this to component() syntax. I know for angular2 there are component queries ( https://angular.io/docs/js/latest/api/core/DirectiveMetadata-class.html ) - some limited version of that would be great for smooth migration path as currently one can use plain old directives with require or "solve" it with lot of

Something in lines of :

app.component('pane'{
   require: {
      tabs: angular.query.parents('tabs',{ optional: true })
   },
   controller: function(tabs){
       tabs.registerPane(this)
   }
});

or

app.component('tabs'{
   require: {
      panes: angular.query.children('pane',{ optional: true })
   },
   controller: function(panes){
       panes.listen(...)
   }
});

Would be very useful and is somewhat vaguely simillar to:

@Component(selector: 'tabs', template: '<content></content>')
class Tabs {
  Query<Pane> panes;

  Tabs(@children this.panes, Logger log) {
    panes.listen((iterable) {
      log(iterable.map((c) => c.value));
      log(iterable.map((c) => c.value));
    });
  }
}

@Component(selector: 'pane')
class Pane {
  @NgAttr("value") String value;
}

Are there any working docs on 1.x directive -> 2.x compoenent migration work i could follow and contribute maybe?

@gkalpak
Copy link
Member

gkalpak commented Jan 12, 2016

Adding support for injecting required controllers into the directive's controller is tracked in #5893 (but there are certain difficulties).

I don't think ng2's ViewQuery is similar to ng1's require (it's kind of the opposite) - the former is looking into the children, the latter is looking through ancestors.

Are you suggesting ng1 should have something similar as ng2's ViewQuery ?

@ev45ive
Copy link
Author

ev45ive commented Jan 12, 2016

Doing this as a special version of Dependency Injector (compoenent aware injector) would be most elegant way. We can make it simillar to router 'resolve' config, where you could Resolve the components you want, use keys as names and then simply inject required controllers using those keys into your controller.

Two ways to approach this:

  1. Allow querying parents in your dependency resolving - compatibile with Angular 1.x 'resolve'
  2. Enable querying children/descendants in DI - simillar way angular2 does it ( https://github.com/angular/Angular2.design/blob/master/lib/directive_injector/discussions/query_api_implementation_notes.txt )

Of course having both would be awsome. ;-) But i guess its a question of eitiher just making ng1 more usable or focusing on migration path to ng2 and making it work simillar to ng2.

Querying children makes some potentially complex things with notyfying you on every ng-repeat there is so you get up-to-date directives instances list ( i.e. this.panes.listen(...) ) where querying parents its stable in that manner - you cant remove parent compoenent without removing your component first.

@ev45ive
Copy link
Author

ev45ive commented Jan 12, 2016

Optional requires might be tricky though... you can't have optional DI instances.. unless we can inject just 'undefined' to controller in case require parent controller doesnt exists?

@petebacondarwin
Copy link
Contributor

Let's move the discussion to #5893

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants