Angular 无法读取属性';价值变化';未定义的
我试图在ViewChild类型的NgModel上实现valueChanges,但在OnInit、AfterViewInit或AfterContentChecked中不断出现相同的错误 hmtl: 我得到的错误Angular 无法读取属性';价值变化';未定义的,angular,rxjs,Angular,Rxjs,我试图在ViewChild类型的NgModel上实现valueChanges,但在OnInit、AfterViewInit或AfterContentChecked中不断出现相同的错误 hmtl: 我得到的错误 index.component.html:37 ERROR TypeError: Cannot read property 'valueChanges' of undefined at IndexComponent.ngOnInit (index.component.ts:70) at c
index.component.html:37 ERROR TypeError: Cannot read property 'valueChanges' of undefined
at IndexComponent.ngOnInit (index.component.ts:70)
at checkAndUpdateDirectiveInline (core.js:24503)
at checkAndUpdateNodeInline (core.js:35163)
at checkAndUpdateNode (core.js:35102)
at debugCheckAndUpdateNode (core.js:36124)
at debugCheckDirectivesFn (core.js:36067)
at Object.updateDirectives (index.component.html:37)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:36055)
at checkAndUpdateView (core.js:35067)
at callViewAction (core.js:35433)
请尝试这样更改
@ViewChild('filteringout',{static:true})filteringout:NgModel
解释
如果将
static
设置为false,则组件总是在视图初始化后及时初始化ngAfterViewInit/ngAfterContentInitcallback
函数。这是与旧配置的标准情况相匹配的,只是它现在是强制的。如果设置了static
,true,初始化将在视图初始化时进行(ngOnInitfor@ViewChild
和@ContentChild
)。请尝试这样更改@ViewChild('filteringout',{static:true})filteringout:NgModel
解释
如果将
static
设置为false,则组件总是在视图初始化后及时初始化ngAfterViewInit/ngAfterContentInitcallback
函数。这是与旧配置的标准情况相匹配的,只是它现在是强制的。如果将static
设置为true,则初始化将在视图初始化时进行(ngOnInitfor@ViewChild
和@ContentChild
)。ngOnInit()
引用DOM元素为时过早。它们可能还不可用。在ngAfterViewInit()
hook中尝试一下。是的,我已经尝试过了,但还是出现了相同的错误filterInout
或filterInput
?你两个都用了。@mbojko那是个打字错误error@MichaelD谢谢,我重试了ngAfterViewInit解决方案,现在它可以工作了。ngOnInit()
引用DOM元素太早了。它们可能还不可用。在ngAfterViewInit()
hook中尝试一下。是的,我已经尝试过了,但还是出现了相同的错误filterInout
或filterInput
?你两个都用了。@mbojko那是个打字错误error@MichaelD谢谢,我重试了ngAfterViewInit解决方案,现在它可以工作了
export class IndexComponent implements OnInit{
@ViewChild('filterInput',{static: false}) filterInput: NgModel
searchTerm: string;
ngOnInit() {
this.filterInput
.valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged()
)
.subscribe(term => {
if(term){
this.getFaqBySearchTerm(term)
}
})
}
index.component.html:37 ERROR TypeError: Cannot read property 'valueChanges' of undefined
at IndexComponent.ngOnInit (index.component.ts:70)
at checkAndUpdateDirectiveInline (core.js:24503)
at checkAndUpdateNodeInline (core.js:35163)
at checkAndUpdateNode (core.js:35102)
at debugCheckAndUpdateNode (core.js:36124)
at debugCheckDirectivesFn (core.js:36067)
at Object.updateDirectives (index.component.html:37)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:36055)
at checkAndUpdateView (core.js:35067)
at callViewAction (core.js:35433)