Avoid define @Input everytime if they are always same

22 views
Skip to first unread message

Andrea Zanti

unread,
Jul 29, 2020, 6:38:43 AM7/29/20
to Angular and AngularJS discussion
Hi everyone,
I have a parent component in which i define a logic. The child component instead is always the same and it receives always the same props from the parent.
I explain better. Here an example:


<parent-component>
   <child-component prop1="prop1" prop2="prop2" ...... again>
   </child-component>
</parent-component>

Form my instance:

<app-login>
    <app-form
      [form]="form"
      [model]="model"
      [fields]="fields"
      [submit]="submit.bind(this)"
      [url]="url">
     </app-form>
</app-login>

N.B: All the props passed on the app-form component are defined in the parent component ( They are alway the same!!)

App form is like a placeholder for the form , logic is always the same: it sends the request to the api , and handles error showing them in the inputs (inline error-forms).
The parent component defines the html thanks to the ng-formly package, and define behaviour after the submit.


Now i want to know if is possible to avoid to redefine everytime all the props.
Something like getting all useful props from parent.

The final result should be like this: 

<app-login>
    <app-form
     </app-form>
</app-login>


Thanks in advance!

bastien lemaire

unread,
Jul 29, 2020, 7:42:01 AM7/29/20
to ang...@googlegroups.com
Hi

You could inject the parent component into the child component and read from/write to to parent component's controller:

@component()
class Parent {}

@Component()
class Child {
  constructor(private parent: Parent) {}
}

Bastien Lemaire


--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/angular/a0f494e6-bf85-4eaf-a389-d769ede82a68n%40googlegroups.com.

Andrea Zanti

unread,
Jul 29, 2020, 9:32:47 AM7/29/20
to Angular and AngularJS discussion
Yes, but my parent component is dynamic. Is possible to have parent ref from child?

bastien lemaire

unread,
Jul 29, 2020, 9:39:13 AM7/29/20
to ang...@googlegroups.com
What you can do is provide the parent:

@Component({
   providers: [
    {provide: DynamicParent, useExisting: forwardRef(() => ParentComponent)}
  ]
})
class ParentComponent implement/extends DynamicParent {}

@Component()
class ChildComponent {
  constructor(private parent: DynamicParent) {}
}

Bastien Lemaire


Andrea Zanti

unread,
Jul 30, 2020, 4:54:13 AM7/30/20
to Angular and AngularJS discussion
Thanks for the response. Could you provide a more detailed response?  I've understood the i can inject the parent component to child to read the instance variable of parent, but i cannot understand how.
Reply all
Reply to author
Forward
0 new messages