【问题标题】:angular2 searching into viewangular2 搜索视图
【发布时间】:2016-09-08 00:15:40
【问题描述】:

我有一个树视图组件。如果尝试使用 ViewChilden 搜索元素,结果为 0。

模板:

<ul class="dropdown">
    <li class="levels-list-item" *ngFor="let level of levelsList">
        <div class="flex justify-flex-start dropdown-item align-center">
            <i class="wrapper fa fa-play rotate-90"
               [ngClass]="{'invisible': !  level.childs?.length}"
               attr.data-id="{{level.id}}"
               #dropdownTrigger></i>
            <div class="dropdown-item-inner flex full-width align-center">
                <div class="name">{{level.title}}</div>
                <div class="tools flex justify-flex-start">
                    <a class="wrapper tool" title="Добавить уровень"
                       [routerLink]="['../LevelAdd', {parentId: level.parentId}]"
                       *ngIf="isPDO(level.type)">
                        <i class="fa fa-plus"></i>
                    </a>
                    <a class="tool wrapper" title="Редактировать уровень"
                       [routerLink]="['../LevelEdit', {parentId: level.parentId, id: level.id}]">
                        <i class="fa fa-pencil"></i>
                    </a>
                </div>
            </div>
        </div>
        <levels-list-item [childs]="level.childs"></levels-list-item>
    </li>
</ul>

我在课堂上使用:

@ViewChildren("dropdownTrigger") dropdownTriggerElms: QueryList<ElementRef>;

元素只存在于儿童中。

其他组件使用 this 调用它

<levels-list-item [childs]="list | async"></levels-list-item>

【问题讨论】:

    标签: twitter-bootstrap angular angular2-template


    【解决方案1】:

    @ViewChildren() (dropdownTriggerElms) 的结果只设置在ngAfterViewInit() 中或之后。如果您尝试从constructor 访问它,它将是null

    对于以后发生的更改,请使用dropdownTriggerElms.changes.subscribe(...)

    ngAfterViewInit() {
      this.dropdownTriggerElms.changes.subscribe(value => console.log('changes', value));
    }
    

    【讨论】:

    • 我知道但没用
    • 不为空,查询长度为0
    • 那么问题出在其他地方,但不看代码很难说。你能创建一个 plunker 吗?
    • 可能数组levelsList 稍后加载?
    • 如果这会改变输出,您可以尝试在本地代码中将this.dropdownTriggerElms.changes.subscribe(value =&gt; console.log('changes', value)); 添加到ngAfterViewInit() 吗?
    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2013-02-06
    • 2019-02-11
    • 2012-07-11
    相关资源
    最近更新 更多