Skip to content

Query annotation doesn't work with typescript and alpha 28 #2916

Closed

Description

@woppa684

... or I'm doing something terribly wrong of course. I have this basic example:

/// <reference path="typings/angular2/angular2.d.ts" />
import {Query, QueryList, Component, View, bootstrap} from "angular2/angular2";

@Component({
    selector: 'd'
})
@View({
    template: ``
})
class D {
}

@Component({
    selector: 'c'
})
@View({
    template: ``,
    directives: [
        D
    ]
})
class C {
    constructor( @Query(D) ds: QueryList<D>) {
        console.log(ds);
    }
}

@Component({
    selector: 'app'
})
@View({
    template: `
    <c>
        <d></d>
        <d></d>
        <d></d>
    </c>
    `,
    directives: [
        C,
        D
    ]
})
class App {
}

bootstrap(App);

When I run this code the logging in the console is this:

QueryList {_results: Array[0], _callbacks: Array[0], _dirty: false}

Why are there no results? Also adding {'descendants': true} has no effect...

Activity

  1. rkirov commented on Jul 10, 2015

    @rkirov
    Contributor

    QueryList is a live list that is updated by change detection. As such construction time is too early to see the D directives.

    To be notified of changes you can attach a callback using onChange as below:

        constructor( @Query(D) ds: QueryList<D>) {
            console.log(ds.length);
            ds.onChange(() => console.log(ds.length));
        }
    
  2. angular-automatic-lock-bot commented on Sep 6, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  3. locked and limited conversation to collaborators on Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions