I am trying to understand property binding and it has been very confusing, I was able to solve my console full of errors, but I cannot figure out why my binding
app.component.html
<div class="container">
<div class="row">
<div class="col-xs-12">
<app-game-control [childTitle]="parentSelector"></app-game-control>
</div>
</div>
app.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
parentSelector = "This shall pass from parent to child" ;
}
game-control.component.html
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-game-control',
templateUrl: './game-control.component.html',
styleUrls: ['./game-control.component.css']
})
export class GameControlComponent implements OnInit {
@Input() childTitle:string ;
title= "Hello from child component";
ngOnInit() {
}
}
game-control.component.html
<div class="row">
<div class="col-xs-12">
{{title}}
</div>
</div>
What I am doing wrong?
childTitle. You are using the propertytitle. Instead you should be using the input property in your game-control.component.html{{childTitle}}game-control.component.htmlyou define your input propertychildTitle. To use the input parameter ingame-control.componentyou have to use the name of that parameter sothis.childTitlein your typescript orchildTitlein your template html.titleingame-control.componentis a totally different property.ngOnChangeshas to be implemented in this component to have the valueGameControlComponent