英文:
display async data in the angular view
问题
我正在从API后端获取数据,并在Angular中将其内容显示在我的视图中,
对于服务,我正在使用可观察对象
export class PropertyService {
apiUrl: string = environment.domain;
constructor(private http: HttpClient) {}
private _propertyDetailedInfo = new Subject();
propertyDetailedInfo$: any = this._propertyDetailedInfo.asObservable();
updatePropertyDetailedInfo(data: any) {
this._propertyDetailedInfo.next(data);
this.propertyDetailedInfo$ = data;
}
gerPropertyDetailedInfo(id: string) {
const res = this.http.get(`${this.apiUrl}/property?id=${id}`);
res.subscribe((val) => {
this.updatePropertyDetailedInfo(val);
console.log(this.propertyDetailedInfo$);
});
}
}
我在组件中订阅它
export class PropertySpecsComponent implements OnInit {
propertyInfo!: any;
constructor(private propertyService: PropertyService) {}
ngOnInit() {
this.propertyService.gerPropertyDetailedInfo('90949d476fb');
this.propertyService.propertyDetailedInfo$.subscribe((res: any) => {
this.propertyInfo = res;
console.log(
` flexibleCancellation : ${this.propertyInfo.flexibleCancellation}`
);
});
}
}
console.log可以工作并显示我正在寻找的内容,甚至可以通过管道显示在视图中,我正在得到TRUE
的值,但是在ng-for中无法读取它
它的代码是
我得到的错误是
我尝试将数据对象静态地插入并将其存储在服务中(我没有使用可观察对象),可以通过json管道显示数据,也可以工作{{ propertyInfo.flexibleCancellation | json }}
,但对于*ngIf="propertyInfo.flexibleCancellation"
不起作用
对我来说,async管道不起作用
*ngIf="propertyInfo.flexibleCancellation | async" 我得到:
英文:
i am fetching the data form an API beck-end and displaying its content in to my view in angular,
for the service i am using observables
export class PropertyService {
apiUrl: string = environment.domain;
constructor(private http: HttpClient) {}
private _propertyDetailedInfo = new Subject();
propertyDetailedInfo$: any = this._propertyDetailedInfo.asObservable();
updatePropertyDetailedInfo(data: any) {
this._propertyDetailedInfo.next(data);
this.propertyDetailedInfo$ = data;
}
gerPropertyDetailedInfo(id: string) {
const res = this.http.get(`${this.apiUrl}/property?id=${id}`);
res.subscribe((val) => {
this.updatePropertyDetailedInfo(val);
console.log(this.propertyDetailedInfo$);
});
}
}
and i am subscribing to it in the Component
export class PropertySpecsComponent implements OnInit {
propertyInfo!: any;
constructor(private propertyService: PropertyService) {}
ngOnInit() {
this.propertyService.gerPropertyDetailedInfo('90949d476fb');
this.propertyService.propertyDetailedInfo$.subscribe((res: any) => {
this.propertyInfo = res;
console.log(
` flexibleCancellation : ${this.propertyInfo.flexibleCancellation}`
);
});
}
}
the console.log works and displays what i am looking and even can be displayed in the view with the pipe i am getting the value of TRUE
for but is cant red the in ng-for
the code for it:
error i am getting:
i trayed to insert the data-object staticly and stored it in service (i did not use observebols) that works displaying the data using json pipe aslo works {{ propertyInfo.flexibleCancellation | json }}
but not for *ngIf="propertyInfo.flexibleCancellation"
using aysinc pipe dos not work for me
*ngIf="propertyInfo.flexibleCancellation | async" i get:
答案1
得分: 1
使用可选链接是一种解决方案,无需使用异步管道
*ngIf="propertyInfo?.flexibleCancellation"
英文:
using optional chaining is a solution no need to use async pipe
*ngIf="propertyInfo?.flexibleCancellation"
答案2
得分: 1
问题分析
可能是在可观察对象的响应被发出之前,您的组件视图已经被渲染。那时,属性 propertyInfo
是未定义的,因为它没有初始值。在您的模板中,对 propertyInfo
调用 flexibleCancellation
会导致 JavaScript 错误,因为 propertyInfo
还没有定义。
可能的解决方案:
1- 使用可选链,就像ibrahimgb的回答中那样
*ngIf="propertyInfo?.flexibleCancellation"
2- 将依赖于 propertyInfo
的模板部分包装在 *ngIf
中
<ng-container *ngIf="propertyInfo">
...
</ng-container>
如果属性未定义,则显示加载旋转器
3- 将空对象设置为 propertyInfo
的初始值
propertyInfo: any = {};
英文:
Problem Analysis
Probably the view of your component is rendered before the response of the observable is emitted. At that time the proprerty propertyInfo
is undefined as it does not have an initial value. In your template the call of flexibleCancellation
on propertyInfo
leads to a javascript error, becasue propertyInfo
is not yet defined.
Possible Solutions:
1- Use optional Chaining like in ibrahimgb's answer
*ngIf="propertyInfo?.flexibleCancellation"
2- Wrap the part of the template that depends on propertyInfo
in an *ngIf
<ng-container *ngIf="propertyInfo">
...
</ng-container>
And display a loading spinner if the property is undefined
3- Set an empty object as initial Value for propertyInfo
propertyInfo: any = {};
答案3
得分: 1
视图在设置您的 propertyInfo
变量的值之前已开始渲染。使用 *ngIf
检查该值是否未定义可能会解决该问题。
英文:
The view is getting rendered before the value of your propertyInfo
variable is set. Checking whether that value is undefined using *ngIf
will solve the issue hopefully.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论