Angular QueryList 在什么情况下查询不到数据
Angular7中自己制作了directive,名称是:NextTabDirective
在一个业务组件中通过QueryList查询获取该类型的DOM元素
然后在组件的 ngAfterViewInit 中打印出来
结果浏览器控制台中打印出来显示没有获取到数据

都有哪些问题会导致使用QueryList查询不到结果?
自己已经确认的问题:
1. 自己制作的directive 名称是 NextTabDirective ,这点肯定没有搞错,名称是复制过来的
2. 该directive 在项目的根模块 app.module.ts 中通过import 引入:
同时在 @NgModule 的 declarations 中声明了。
3. 业务组件的ts文件中也在头部(最上面)引入了:
4. 项目结构是:
app
|--->app.module.ts
|--->shared\ccDirective\next-tab.directive.ts
|--->pages\order\order.component.html <-- 业务组件html模板文件
|--->pages\order\order.component.ts <-- 业务组件ts文件
5. 下面是自定义directive的全部代码
6. order.component.html 的标签中使用自定义directive的代码如下:
疑问:
同样的方法,单独制作一个项目测试通过的,把这些代码搬到我开发中的项目里就获取不到数据
-- 通过
获取不到
我哪里错了?thanks for any help..
在一个业务组件中通过QueryList查询获取该类型的DOM元素
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>;然后在组件的 ngAfterViewInit 中打印出来
结果浏览器控制台中打印出来显示没有获取到数据

都有哪些问题会导致使用QueryList查询不到结果?
自己已经确认的问题:
1. 自己制作的directive 名称是 NextTabDirective ,这点肯定没有搞错,名称是复制过来的
2. 该directive 在项目的根模块 app.module.ts 中通过import 引入:
import { NextTabDirective } from './shared/ccDirective/next-tab.directive';同时在 @NgModule 的 declarations 中声明了。
3. 业务组件的ts文件中也在头部(最上面)引入了:
import { NextTabDirective } from 'src/app/shared/ccDirective/next-tab.directive';4. 项目结构是:
app
|--->app.module.ts
|--->shared\ccDirective\next-tab.directive.ts
|--->pages\order\order.component.html <-- 业务组件html模板文件
|--->pages\order\order.component.ts <-- 业务组件ts文件
5. 下面是自定义directive的全部代码
import { Directive, HostListener, ElementRef } from '@angular/core';
@Directive({
selector: '[next-tab]'
})
export class NextTabDirective {
self:any;
nextControl:any;
@HostListener("keydown.enter", ["$event"])
onEnter(event: KeyboardEvent) {
if (this.nextControl) {
if (this.nextControl.focus) {
this.nextControl.focus();
this.nextControl.select();
event.preventDefault();
return false;
}
}
}
constructor(private control: ElementRef) {
this.self=control.nativeElement;
}
}
6. order.component.html 的标签中使用自定义directive的代码如下:
<input type="text" next-tab>疑问:
同样的方法,单独制作一个项目测试通过的,把这些代码搬到我开发中的项目里就获取不到数据
-- 通过
@ViewChildren(NextTabDirective) inputs: QueryList<NextTabDirective>;获取不到
我哪里错了?thanks for any help..
...全文
相关推荐
Angular2学习教程之组件中的DOM操作详解
主要给大家介绍了Angular2学习教程之组件中DOM操作的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来看看吧。
angular-viewChild-and-viewChildren:使用StackBlitz:high_voltage:创建
角u8dolz
angular6的table组件开发的实现示例
主要介绍了angular6的table组件开发的实现示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Angular使用动态加载组件方法实现Dialog的示例
主要介绍了Angular使用动态加载组件方法实现Dialog的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Angular2 组件交互实例详解
Angular2应用程序实际上是有很多父子组价组成的组件树,因此,了解组件之间如何通信,特别是父子组件之间,对编写Angular2应用程序具有十分重要的意义。下面通过本文给大家介绍Angular2 组件交互知识,感兴趣的朋友一起看看吧



