An open API service indexing awesome lists of open source software.

https://github.com/leolanese/angular-component-communication

Angular (19+) Observables and Signal using @Input/@Output playground and Signal Input/Output
https://github.com/leolanese/angular-component-communication

angular httpresource observable rxjs signal typescript

Last synced: 19 days ago
JSON representation

Angular (19+) Observables and Signal using @Input/@Output playground and Signal Input/Output

Awesome Lists containing this project

README

        

# Angular (19+) Observables and Signal using @Input/@Output playground and Signal Input/Output 🔊

## Demo

![Demo](./src/app/assets/demo.png)

```js
- /`child-contructor-input-onchanges`
Accessing passed values from contructor() (not safe), @Input() and ngOnChanges() with SoC

- /`child`
passing P <-> C @Input/@Output with SoC

- Child-observable: Passing Observable from P -> C + Reusable Generic Service
```

---

## NOTES:
> When passing values from a P -> C component using the `@Input`, `these values are not available in the constructor` = Avoids running Angular-specific logic or accessing @Input properties, as they are not yet set.

> If you need to react to changes in @Input values beyond initialization, consider using the `ngOnChanges()` lifecycle hook

### constructor()
- Called first, before any Angular lifecycle hooks.
- Used to initialise the component instance.
- Runs before Angular has fully initialized the component.
- `Not safe access @Input values`

### ngOnInit()

- called after the constructor, after the first ngOnChanges()
- `Safe for access @Input` values
- Runs after the constructor and after Angular sets up the component's bindings.

---

## @for instead *ngFor()

> The key difference is that `@for` loops over the array value directly, while `*ngFor` gives you the array element value.

Feature: Syntax
- `ngFor`: `*ngFor="let item of items"`
- `@for`: `@for="let i from X to Y"`

```js
// instead of


  • {{ suggestion }}

// better do


    @for (let suggestion of suggestions) {
  • {{ suggestion }}

  • }

```

## @for with | async pipe

```js
// instead




Loading...

// better do
@for(user of users$ | async; track user){

} @empty {

Loading...


}
```

## @for with $index

```js
@Component({
template: `
@for(fruit of fruits; track fruit; let index = $index) {

{{ fruit }} {{ index }}

}

`,
})
export class FruitComponent {
fruits = ['apple', 'lemon'];
}
```

## @for with $first and $last

```js
@Component({
template: `
@for(fruit of fruits; track fruit; let first = $first, last = $last) {

First: {{ first }}: Last {{ last }}

}
`,
})
export class FruitComponent {
fruits = ['apple', 'lemon'];
}

//

First: true: Last false

//
First: false: Last true

```

---

### :100: Thanks!
#### Now, don't be an stranger. Let's stay in touch!


leolanese’s GitHub image

##### :radio_button: Linkedin: LeoLanese
##### :radio_button: Twitter: @LeoLanese
##### :radio_button: DEV.to: Blog
##### :radio_button: Questions / Suggestion / Recommendation: [email protected]