小编mto*_*nev的帖子

当动态添加二级子级时,Angular2 @ ViewChildren/@ViewQuery的QueryList未更新

我想创建一个目录树,并且无论级别如何都可以访问所有项目,但是当我动态添加二级子级时,@ ViewChildren/@ViewQuery的QueryList不会更新:

import {bootstrap} from 'angular2/platform/browser';
import {Component, View, QueryList,ViewQuery, Query,ViewChildren} from 'angular2/core';


@Component({
    selector: 'item',
})
@View({
    template: `
        <button (click)="addChild()">Add Child</button>
        <ng-content></ng-content>
        <div *ngFor="#child of children; #i = index">
          <item> {{child.name}}</item>
        </div>
    `
})
export class Item {
    //public children = [ { name: 'Child 1', age: 22 } ];
    public children = [];
    addChild() {
        this.children.push({ name: 'Child' + (this.children.length + 1), age: 18 });
    }
}

@Component({
    selector: 'my-app'
})
@View({
    directives: [Item],
    template: `
        <button …
Run Code Online (Sandbox Code Playgroud)

angular

7
推荐指数
1
解决办法
5231
查看次数

标签 统计

angular ×1