不知道为什么会在这里发生,我从我的组件中收到此错误:
ERROR TypeError: Cannot read property 'timezone_offset' of undefined
at Object.eval [as updateRenderer] (SettingsComponent.html:16)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14377)
at checkAndUpdateView (core.js:13513)
at callViewAction (core.js:13858)
at execComponentViewsAction (core.js:13790)
at checkAndUpdateView (core.js:13514)
at callViewAction (core.js:13858)
at execEmbeddedViewsAction (core.js:13816)
at checkAndUpdateView (core.js:13509)
at callViewAction (core.js:13858)
有趣的是,它仍然在模板中显示正确的字符串,并且 ng-cli 中没有错误。这是产生错误的组件的代码:
import { Component, OnInit } from '@angular/core';
import { GetProfileService } from '../../services/get-profile.service';
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html'
})
export class SettingsComponent implements OnInit {
results: any;
profile: any;
constructor(private getProfileService: GetProfileService) { }
ngOnInit() {
this.getProfileService.profileAPI().subscribe(
data => {
this.results = data
this.profile = this.results
console.log(this.profile)
}
);
}
}
我现在只是{{ profile.timezone_offset }}
在我的模板上用作唯一的东西......
似乎您的数据profile
在呈现时尚未加载到对象中。只有在异步操作完成后,profile
对象内的数据才被设置。?.
当您的对象在异步调用上运行时应该使用。timezone_offset
将仅在profile
定义对象时进行评估。
由于异步操作,您可能会收到该错误。尝试以下
{{ profile?.timezone_offset }}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句