Angular 角度ngIf:标识符';长度';没有定义
我的组件具有属性transfers$,它是一个可观察的对象,可以与包含错误消息的数组或对象一起使用。 当转账美元为转账[]时,我只能输入[项目]。但即使是使用条件Angular 角度ngIf:标识符';长度';没有定义,angular,typescript,angular-ng-if,angular-arrays,angular-template-variable,Angular,Typescript,Angular Ng If,Angular Arrays,Angular Template Variable,我的组件具有属性transfers$,它是一个可观察的对象,可以与包含错误消息的数组或对象一起使用。 当转账美元为转账[]时,我只能输入[项目]。但即使是使用条件 <app-checklist *ngIf="transfers?.length > 0" [items]="transfers"></app-checklist> 组件技术 import { Component, OnInit } from '@angular/c
<app-checklist *ngIf="transfers?.length > 0" [items]="transfers"></app-checklist>
组件技术
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { TransfersService } from './TransfersService';
export interface AppResponseError = {code:number,title:string,message:string};
export interface Transfer = {id:number,title:string};
@Component({
selector: 'app-transfer',
})
export class TransferComponent implements OnInit {
transfers$: Observable<Transfer[] | AppResponseError>;
constructor(private transfersService: TransfersService) {
this.transfers$ = this.transfersService.transfers$;
}
ngOnInit() {
this.transfers$.subscribe((data) => {
console.log(data);
});
}
}
从'@angular/core'导入{Component,OnInit};
从“rxjs”导入{Observable};
从“./TransfersService”导入{TransfersService};
导出接口AppResponseError={code:number,title:string,message:string};
导出接口传输={id:number,title:string};
@组成部分({
选择器:“应用程序传输”,
})
导出类TransferComponent实现OnInit{
转移$:可见;
构造函数(私有传输服务:传输服务){
this.transfers$=this.transfersService.transfers$;
}
恩戈尼尼特(){
此.transfers$.subscribe((数据)=>{
控制台日志(数据);
});
}
}
您不需要额外的*ngIf
<div *ngIf="transfers$ | async; let transfers; else loadingTpl">
然后像这样在模板中使用它
<app-checklist *ngIf="isDataAvailable(transfers)" [items]="transfers"></app-checklist>
…这里有什么
是的,我需要以某种方式进行测试,因为当传输$的值不是数组时,我的应用程序中的内部组件也需要有效的数组。我在帖子中添加了更多信息,以便更清楚地了解更新情况。请检查。。
<div *ngIf="transfers$ | async; let transfers; else loadingTpl">
isDataAvailable(arr){
return Array.isArray(arr) && arr.length >0
}
<app-checklist *ngIf="isDataAvailable(transfers)" [items]="transfers"></app-checklist>
<div *ngIf="transfers$ | async; let transfers; else loadingTpl">
<app-checklist *ngIf="isDataAvailable(transfers)" [items]="transfers"></app-checklist>
<div *ngFor="let transfer of transfers">
..something here
</div>
<ng-container *ngTemplateOutlet="emptyList; context: {length: transfers.length}"></ng-container>
<ng-container *ngTemplateOutlet="error; context:transfers"></ng-container>
</div>