Performance: @Input decorator for parent to child
This affects how data is passed and rendered between parent and child components, impacting rendering speed and interaction responsiveness.
Parent template: <child-comp [data]="parentData" *ngIf="shouldShowChild"></child-comp> Child component: @Input() data: any; // Parent updates data only when necessary, uses OnPush change detection
Parent template: <child-comp [data]="parentData"></child-comp> Child component: @Input() data: any; // Parent updates parentData frequently even if child doesn't need it
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Frequent input updates without OnPush | Many DOM updates | Multiple reflows | High paint cost | [X] Bad |
| Input updates with OnPush and immutable data | Minimal DOM updates | Single reflow | Low paint cost | [OK] Good |
@Input decorator in Angular?@Input@Input decorator is used to mark a property in a child component that can receive data from its parent.@Input passes data parent to child [OK]@Input means data flows into child [OK]@Input with @Output@Input sends data up to parent@Input with services or routing@Input property named title in a child component?@Input syntax@Input() decorator before the property declaration.@Input() with the property name and type. Options A and D miss the decorator syntax, and C uses @Output() which is for event emitters, not inputs.@Input() [OK]@Input() decorator before property [OK]@Output instead of @Input@Input@Component({ selector: 'child-comp', template: '{{name}}' })
export class ChildComponent {
@Input() name!: string;
}<child-comp [name]="parentName"></child-comp>
parentName = 'Angular', what will the child component display?name property using property binding [name].{{name}}, which will be replaced by the value passed, 'Angular'.@Input [OK]{{name}} to show literally@Component({ selector: 'child-comp', template: '{{data}}' })
export class ChildComponent {
@Input() data: string;
}<child-comp data="parentData"></child-comp>
[data].data="parentData" without brackets, which passes the literal string 'parentData', not the variable's value.[data] -> Option C[inputProp] to pass variables [OK]@Input [OK]@Input with @Outputuser = {name: 'Alice', age: 30}. You want to pass only the name to a child component using @Input. Which is the best way to do this in the parent template?name property from the user object, not the whole object.user.name to the child's userName input. <child-comp [userName]="user"></child-comp> passes the whole object, not just the name. <child-comp userName="user.name"></child-comp> passes the string literal 'user.name' instead of the value. <child-comp [userName]="user['age']"></child-comp> passes the age, not the name.[inputProp]="object.prop" [OK][inputProp]="object.prop" [OK]Pass the test and this pattern moves unknown