Skip to content

Delay rendering until required @Input are ready #28403

@okeydoky

Description

@okeydoky

🚀 feature request

Relevant Package

This feature request is for @angular/core

Description

A clear and concise description of the problem or missing capability...

When you are writing a component, there are usually some vital inputs need to be ready for your component to work/render properly. The problem is these inputs may not be ready at the component instantiate time. To guard this scenario, the component author usually needs to provide some default values to these inputs, so the null/undefined inputs field won't break your component. But not every input type has an obvious default. For example, an object input will require to write a long object literal or your need to add a bunch of safe operator ?. in your template. A function as an input is another example.

Describe the solution you'd like

If you have a solution in mind, please describe it.

If we can have a decorator, say @required and use it like:
export class SomeComponent { @Required @Input requiredProperty; }

and usage of the component:
<some-component [requiredProperty]="asyncData"></some-component>
will be equivalent to
<some-component *ngIf="requiredProperty" [requiredProperty]="asyncData"></some-component>

I believe there are many benefits if we have this capability:

  • It specifies a clear contract what component author expects component users to provide.
  • Component author no longer needs to worry the inputs are in inconsistent state while waiting they arrive.
  • It relieve the component users the burdens to write those *ngIf check. Image if a component has 4 or more required inputs.
  • The component users may even get a compiler warning/error when they forget to bind some required inputs.

Am I too naive? Does this make sense?

Describe alternatives you've considered

Have you considered any alternative solutions or workarounds?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtime

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions