Im doing a simple app, but for some reason, the parent methods, that Im invokes in the template, are not working.
- Am I missing something?
- Seams like the template is executed before the TS is ready. Is this possible? If yes, how to prevent it?
Template:
<app-search-list
[dataResults]="Pokemons"
[pokemonArray]="pokemonDataArray"
(dataPokemonEvent)="receivePokemonData($event)"
></app-search-list>
<app-search (dataEvent)="receiveCustomData($event)">
<p style="background-color: yellow">share data: {{ msg }}</p>
</app-search>
typescript
import {
Component,
Input,
ViewChild,
} from '@angular/core';
import { SearchListComponent } from './search-list/search-list.component';
import { SearchComponent } from './search/search.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
})
export class AppComponent implements AfterContentInit {
@ViewChild(SearchListComponent) dataSearchList: any;
@ViewChild(SearchComponent) dataSearch: any;
@Input() msg: string = '{msg} not implemented';
@Input() pokemonDataArray: Object[] = [];
Pokemons = [ ];
constructor() {}
receiveCustomData(event: any) {
this.msg = event;
alert('receiveCustomData');
console.log('receiveCustomData');
}
receivePokemonData(event: any) {
console.log('receivePokemonData');
alert('receivePokemonData');
this.pokemonDataArray = event;
}
ngAfterContentInit(): void {
// this.receiveCustomData('Test');
}
}
2
Answers
I see a couple of things you can try. Be more specific with the type of @ViewChild, and at the @Input(), you’re trying to interpolate but, since the variable
msg
hasn’t been declared. I’m guessing you declared it in the parent, so try to fix that, either by removing msg, or passing the string that you need, and not defining it in the child.You need to look into
SearchListComponent
andSearchComponent
. Check out what triggersdataPokemonEvent
anddataEvent
.Why are you declaring
ViewChild
s – you are not using it inAppComponent
.If you can chare also code of
SearchListComponent
andSearchComponent
.