【发布时间】:2017-03-18 14:05:23
【问题描述】:
我知道以前有人问过这个问题,但没有一个选择的答案对我有用。
我正在尝试使用 @ViewChild 从 dom 获取我的 ng-select。而且它总是返回 undefined。
这是main.html中的选择:
<ng-select id = "user-select"
#userSelect
[allowClear]="true"
[items]="singleSignOnUsers"
[disabled]="disabled"
placeholder="No user selected">
</ng-select>
这是我的组件
import { Component, AfterViewInit, ViewChild } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-main',
templateUrl: '../app/main.html',
providers: [ApiHttpService, UserAdministrationService]
})
export class AppComponent {
@ViewChild('userSelect') userSelect;
ngAfterViewInit() {
alert(this.userSelect);
}
}
我在这里错过了什么?
更新:哦,我的灵魂! 我明白了为什么这在应该的时候不起作用。
我将整个视图包裹在 div 和 ngSwitch 中。如果我将其移出,我可以访问它们。
现在我不知道如何在ngSwitch 中访问它们。但我不必使用ngSwitch。
<div [ngSwitch]='loading'>
<div *ngSwitchCase="false">
...
<ng-select id="user-select"
#userSelect
[allowClear]="true"
[items]="singleSignOnUsers"
[disabled]="disabled"
placeholder="No city selected">
</ng-select>
...
</div>
<div
【问题讨论】:
-
这应该可以。你能在 Plunker 中复制吗?
-
我从未使用过 plunker。我会看看我能不能弄明白。
-
Plunker 在
[ New | v ]按钮下提供了一个现成的Angular2 TS 模板。 -
ng-select 真的出现了吗?组件是否在模块中注册?
-
是的,ng-select 确实出现了。仅使用 div 或常规选择而不是 ng-select 时,我也会遇到同样的问题
标签: angular typescript