模板中的Angular2 ViewChild
Angular2 ViewChild in template
我有以下视图(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);
}
}
我想在编辑单击上获得inline-editor
以调用编辑函数,但我无法获得此对象的引用。我尝试了以下viewChildren:
@ViewChildren('nodeEdit') nodeEdits: any;
但它总是未定义的。得到tree
或treeNodeTemplate
工作如预期。
希望有人能帮助我:)
我尝试在编辑按钮上单击this.nodeEdits
,因此在此之前调用ngAfterViewInit()
。
我尝试从我的兴趣控制器访问它它是视图的控制器
不能在ngOnInit()
或构造函数中访问nodeEdits
。
ngAfterViewInit()
被称为this.nodeEdits
时设置。
相关文章:
- 如何在Angular2中使用jQuery插件
- 不能从angular2中的子组件指定父组件中的数组
- 如何为生产构建angular2应用程序
- 使用javascript协调angular2组件
- Angular2将组件方法导入到另一个组件中
- Angular2在装载前
- Angular2/JavaScript-如何显示localStorage保存的所有变量并获取所有键的总长度
- 基于路由的CSS样式-Angular2
- Angular2 ngFor—已选择集
- Angular2他们如何保存到缓存
- Angular2 rc1, where is shims_for_IE
- angular2中的全局服务不工作
- 如何在重建URL后从URL中传递过滤值,目的是使用Angular2和Typescript实现无限滚动
- Angular2-RadioButtonState未按预期工作
- Angular2-*ngIf:如何显示特定元素
- Angular2-TypeError:无法读取属性'Id'(Typescript)中未定义的
- Angular2/JavaScript-按1递增/递减(单击)
- Angular2组件未从视图中呈现javascript
- 如何将此JS翻译成Angular2
- 模板中的Angular2 ViewChild