我有一个位于应用程序顶层的指令,并通过查询元素document.querySelector并将它们传递给fromEvent. 问题是,当指令中的代码ngAfterViewInit运行时,DOM 尚未完全呈现,因此元素查询返回 null。
setTimeout我可以在任意时间内运行代码,但这似乎不是一个可持续且灵活的解决方案。
关于如何优雅地处理这个问题有什么想法吗?
现在,我的代码看起来像这样的一些变体:
ngAfterViewInit() {
const trackedEl = document.querySelector('#myInput');
const input$ = fromEvent(trackedEl, 'input')
.pipe(
map((inputEvent: any) => inputEvent.target.value)
)
.subscribe((v) => {
console.log(`Got to the observable:: ${v}`, count++);
})
}
Run Code Online (Sandbox Code Playgroud) 我一直试图用业力进行一些测试但是却无法让它发挥作用.在尝试使用我的应用程序后,我尝试使用最简单的可能测试运行它,然而,它仍然没有完成.
这是我的karma.conf.js文件:
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
// 'app/bower_components/angular/angular.js',
// // 'app/bower_components/angular/angular-mocks.js',
// 'app/scripts/*.js',
// 'app/scripts/controllers/main.js',
// // 'test/mock/**/*.js',
// 'test/spec/**/*.js',
// // 'app/scripts/**/*.js,'
'testing.js'
],
// list of files / patterns to exclude
exclude: ['angular-scenario.js'],
// web server port …Run Code Online (Sandbox Code Playgroud)