Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 : ContentChildren+QueryList error

Tags:

angular

I want use @ContentChildren in Angular2 to navigate in a wizard. But when i use it, i have this error : "zone.js?fad3:158 Uncaught Error: Can't construct a query for the property "steps" of "ExtendedWizard" since the query selector wasn't defined."

wizard.component.ts

import { Component, ContentChildren, QueryList } from '@angular/core';

@Component({
    selector: 'extended-wizard',
    template: `
            <ul>
                <li *ngFor="let step of steps">
                    {{step.title}}
                </li>
            </ul>
            <ng-content></ng-content>
    `
})
export class ExtendedWizard {
    @ContentChildren(ExtendedStep) steps: QueryList<ExtendedStep>;
}

@Component({
    selector: 'extended-step',
    template: `
        <fieldset>
            {{title}}
            <ng-content></ng-content>
        </fieldset>
    `
})
export class ExtendedStep {
    title: string;
}

app.component.html

<extended-wizard>
    <extended-step title="Step 1"></extended-step>
    <extended-step title="Step 2"></extended-step>
</extended-wizard>

Thanks for your help

like image 667
di99er Avatar asked Nov 28 '25 05:11

di99er


1 Answers

You need to move the class declaration export class ExtendedStep above it's first use, otherwise it will not be recognized. (only if you have several classes in the same file).

If you move

export class ExtendedStep {
    title: string;
}

just below the imports, it should work.

like image 127
Günter Zöchbauer Avatar answered Nov 30 '25 21:11

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!