Pass inputs to nested component in Angular 2

后端 未结 3 454
难免孤独
难免孤独 2021-02-03 21:42

How can the attributes be transparently translated from wrapper component to nested component?

Considering that there is

const FIRST_PARTY_OWN_INPUTS = [         


        
3条回答
  •  一生所求
    2021-02-03 21:57

    I think this can be boiled down to a more basic problem without Angular2 at all. When you have a function that takes a lot of parameters, it's annoying and error-prone to have to specify all those parameters every time you want to use it. The problem gets worse when there's an intermediate function that doesn't care about those parameters at all - you find yourself adding parameters to the intermediate function just so it can pass it to the inner function. Yeargh!

    There are a few patterns for dealing with this. My favorite is to fully instantiate the inner function and pass the instance already loaded up with the former pass-through parameters embedded in it. I think http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/ is a nice post about how to do that in Angular 2 using @ViewChild and @ContentChild. Another strategy is to wrap all of the pass-through parameters up in a single object, so at least there's only one parameter to pass through. That also helps when you want to add more parameters - since they're already being wrapped up and passed through opaquely, your pass-through code doesn't need to change.

提交回复
热议问题