javascript – 模板中的Angular2 ViewChild

我有以下视图(interests.component.html):

<Tree #tree [nodes]="nodes" [options]="treeOptions">
  <template #treeNodeTemplate let-node>
    <inline-editor #nodeEdit type="text" [(ngModel)]="node.data.name" (onEdit)="beginEditable($event)" (onSave)="saveEditable($event)" name="node{{node.data.id}}" size="8"></inline-editor>

      <a (click)="addNode(node)"><i class="pull-right btn btn-xs icon-plus"></i></a>
      <a (click)="editNode(node)"><i class="pull-right btn btn-xs icon-note"></i></a>
      <a (click)="deleteNode(node)"><i class="pull-right btn btn-xs icon-trash"></i></a>
  </template>
</Tree>

我的组件看起来像这样(interests.component.ts):

import { Component,ViewChild,ViewChildren,QueryList } from '@angular/core';
import * as _ from "lodash";
import * as $from "jquery";

import { AppState } from '../app.service';
import {TreeModule,TreeNode,TREE_ACTIONS,KEYS,IActionMapping } from 'angular2-tree-component';
import {InlineEditorComponent} from 'ng2-inline-editor';

@Component({
  selector: 'interests',providers: [],styleUrls: ['./interests.component.scss'],templateUrl: './interests.component.html'
})
export class Interests {
  @ViewChild('tree') tree: any;
  @ViewChildren('nodeEdit') nodeEdits: any;

  /** Level multiplikator for tree id creation. */
  public static readonly LEVEL_MULTIPLIKATOR = 100000;

  /** Last inline text element which is/was in edit mode. */
  public lastSelectedEditElement: any;

  /** Id of last selected edit node. */
  public lastSelectedEditId: number;

  /** Options for the tree component. */
  treeOptions = {
    allowDrag: true
  };

  /** contains the tree nodes. */
  nodes: any;

  ngAfterViewInit() {
    console.log(this.nodeEdits);
  }

}

我想在编辑单击上获取内联编辑器以调用编辑功能,但我无法获得对此对象的引用.
我试过以下viewChildren:

@ViewChildren('nodeEdit') nodeEdits: any;

但它总是未定义的.
获取树或treeNodeTemplate按预期工作.

希望有人能帮助我:)

更新

我尝试在编辑按钮上单击访问this.nodeEdits,以便在此之前调用ngAfterViewInit().

我尝试从我的兴趣控制器访问它,这是视图的控制器.

解决方法

您无法访问ngOnInit()或构造函数中的nodeEdits.仅当调用ngAfterViewInit()时才设置this.nodeEdits.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...