#New with angular, I need some explanation about binding

11 messages · Page 1 of 1 (latest)

pastel bridge
#

Hello Folks ! I am new with Angular and I am a bit confused about binding.

So I saw that it serve to link parent/ Children property to share to the children the properties via signals/ @Input, right ?

but I am a bit confused. On my tutorial the dev create an object in TS to define the properties of a "Monster" Object.

a playing-card is the "children" component as the app is the parent component

app --> Playing-card

the future purpose is to create severals differents Playing-card. (Each one a different Monster)

  • so Monster properties are typed in Monster.model.ts (an extern file in app folder)
  • in Playing-cards.ts the Monster object is instantiated in the component class
  • in app.ts
export class App {
  monster1!: Monster;

  constructor(){
    this.monster1 = new Monster();
    this.monster1.name = "Pik"
    this.monster1.hp = 40;
    this.monster1.figureCaption = "N°004"
  }

and in app.html:

<div id="card-list">
    <app-playing-card/>
    <app-playing-card [monster] = "monster1"/>
</div>```


So I guess after in order to create several cards a JSON will be used and we just need to use a for loop in the app.html or in the constructor of the app class.

But I am not sure to understand everything:

- What is the binding definition with the [] around the property in the app html ?

- I am a bit confused with the TS why should we instantiated in the app constructor a monster1 ?

- how is working the compilation  as I declare an App class with a constructor ? the constructor allow to create the monster each time App is instantiated but when should App be instantiate ? when the DOM is generated ?
I miss the point.

Why in playingCard.ts so my card component I have to instantiate also a new Monster ?

```ts
export class PlayingCard {
  @Input() monster : Monster = new Monster();
}```

Thanks in advance for your feedback.

Sorry it's a bit the mess... 😅
lilac ocean
#

Your tutorial seems outdated (it still uses @Input) and doesn't seem to use best practices. I strongly encourage you to read something more up-to-date and reliable, like the official Angular documentation.

golden ruin
#

app is the startup component. You can see it as bootstrap in app.config.ts.
app needs to display Playing-card, so it creates an instance of the Playing-card component.
Like any object, the constructor is called upon creation.
And the monster1 variable is constructed.

monster1 is constructed in app and passed as an input parameter of the child component PlayingCard)

This way, playyingCard displays the card it receives.
The parent can send it any card that follows the Monster model.

You can create a Monster model Array by hand or retrieve a Monster model Array via an http request.

pastel bridge
pastel bridge
pastel bridge
pastel bridge
#

and I guess in fact you declare all the dynamic activity of the component in it's own class in component.ts

golden ruin
pastel bridge
#

ok so [] is like to say "This property's value will be given by the parent" and then you pass the parent's variable name as the value ?

lilac ocean
# pastel bridge ok so [] is like to say "This property's value will be given by the parent" and ...

You don't pass a parent's variable name. You pass an Angular expression, which must be of the appropriate type. In Angular expressions, variables (such as monster1) refer to properties of the current component. But you could have something like

monsters = {
  big: new Monster(),
  small: [
    new Monster(),
    new Monster()
  ]
}

and use, in your template:

Big monster: <app-playing-card [monster]="monsters.big" />
First small monster: <app-playing-card [monster]="monsters.small[0]" />
Second small monster: <app-playing-card [monster]="monsters.small[1]" />