1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Angular 2 @ViewChild annotation returns undefined

Discussão em 'Angular' iniciado por Andrea Ialenti, Agosto 11, 2025.

  1. I am trying to learn Angular 2.

    I would like to access to a child component from a parent component using the @ViewChild Annotation.

    Here some lines of code:

    In BodyContent.ts I have:

    import { ViewChild, Component, Injectable } from 'angular2/core';
    import { FilterTiles } from '../Components/FilterTiles/FilterTiles';

    @Component({
    selector: 'ico-body-content',
    templateUrl: 'App/Pages/Filters/BodyContent/BodyContent.html',
    directives: [FilterTiles]
    })
    export class BodyContent {
    @ViewChild(FilterTiles) ft: FilterTiles;

    public onClickSidebar(clickedElement: string) {
    console.log(this.ft);
    var startingFilter = {
    title: 'cognomi',
    values: [ 'griffin', 'simpson' ]
    }
    this.ft.tiles.push(startingFilter);
    }
    }


    while in FilterTiles.ts:

    import { Component } from 'angular2/core';

    @Component({
    selector: 'ico-filter-tiles',
    templateUrl: 'App/Pages/Filters/Components/FilterTiles/FilterTiles.html'
    })
    export class FilterTiles {
    public tiles = [];

    public constructor(){};
    }


    Finally here the templates (as suggested in comments):

    BodyContent.html

    <div (click)="onClickSidebar()" class="row" style="height:200px; background-color:red;">
    <ico-filter-tiles></ico-filter-tiles>
    </div>


    FilterTiles.html

    <h1>Tiles loaded</h1>
    <div *ngFor="#tile of tiles" class="col-md-4">
    ... stuff ...
    </div>


    FilterTiles.html template is correctly loaded into ico-filter-tiles tag (indeed I am able to see the header).

    Note: the BodyContent class is injected inside another template (Body) using DynamicComponetLoader: dcl.loadAsRoot(BodyContent, '#ico-bodyContent', injector):

    import { ViewChild, Component, DynamicComponentLoader, Injector } from 'angular2/core';
    import { Body } from '../../Layout/Dashboard/Body/Body';
    import { BodyContent } from './BodyContent/BodyContent';

    @Component({
    selector: 'filters',
    templateUrl: 'App/Pages/Filters/Filters.html',
    directives: [Body, Sidebar, Navbar]
    })
    export class Filters {

    constructor(dcl: DynamicComponentLoader, injector: Injector) {
    dcl.loadAsRoot(BodyContent, '#ico-bodyContent', injector);
    dcl.loadAsRoot(SidebarContent, '#ico-sidebarContent', injector);
    }
    }


    The problem is that when I try to write ft into the console log, I get undefined, and of course I get an exception when I try to push something inside the "tiles" array: 'no property tiles for "undefined"'.

    One more thing: FilterTiles component seems to be correctly loaded, since I'm able to see the html template for it.

    Any suggestions?

    Continue reading...

Compartilhe esta Página