Angular 带有3个or子句和空对象的NgIF
看起来我不知道如何创建一个包含3个或多个子句的NgIf,但我找不到错误之处或任何可复制的示例:Angular 带有3个or子句和空对象的NgIF,angular,Angular,看起来我不知道如何创建一个包含3个或多个子句的NgIf,但我找不到错误之处或任何可复制的示例: *ngIf="owner === photo.calbum.community.user.id || owner === photo.album.user.id || isAdmin == true" 然后我有一个JSON对象,它在一些记录中,但有时它有一张相册中的照片(或者它是空的),或者有时它有一个属于社区的公共相册(或者它是空的,因为照片属于相册) 在这两种情况下,它们都属于一个用户,但通过不
*ngIf="owner === photo.calbum.community.user.id || owner === photo.album.user.id || isAdmin == true"
然后我有一个JSON对象,它在一些记录中,但有时它有一张相册中的照片(或者它是空的),或者有时它有一个属于社区的公共相册(或者它是空的,因为照片属于相册)
在这两种情况下,它们都属于一个用户,但通过不同的对象。但是其中一个总是空的,我需要检查整个JSON文件
谢谢。如果任何对象或其属性null
*ngIf="owner === photo?.calbum?.community?.user?.id || owner === photo?.album?.user?.id || isAdmin == true"
另一个选项是创建一个属性或函数,您只能检查该值,但需要处理检查值是否为null
get state() : boolean{
if (this.isAdmin ) {
return true
} else if (this.photo && this.photo.calbum) {
return this.owner === this.photo.calbum.community.user.id
} else if (this.photo && this.photo.album) {
return this.owner === this.photo.album.user.id
} else {
return false;
}
}
模板
<div *ngIf="state">/<div>
/
试试这个。。因为我认为您的条件似乎适合html中的布尔值,那么为什么不尝试使用组件呢
isTrue:boolean;
constructor(){
if((this.photo != null && (this.owner === this.photo.calbum.community.user.id || this.owner === this.photo.album.user.id)) || this.isAdmin == true){
this.isTrue=true;
}else {
this.isTrue=false;
}
}
get my(){
return //all your conditions
}
*ngIf="my"
并在html中添加此*ngIf=isTrue或者您可以根据需要在方法中而不是构造函数中执行,并从html
调用它。您可以使用getter
在您的组件中
isTrue:boolean;
constructor(){
if((this.photo != null && (this.owner === this.photo.calbum.community.user.id || this.owner === this.photo.album.user.id)) || this.isAdmin == true){
this.isTrue=true;
}else {
this.isTrue=false;
}
}
get my(){
return //all your conditions
}
*ngIf="my"
或
语句很好,您有什么问题吗?一般来说,创建get参数是一种更好的做法,比如说“shouldDisplay”,它将计算这个布尔语句。您能给出一个最小的示例(stackblitz、plunkr等)吗为什么不在组件中添加一个布尔变量,并在构造函数中为其分配条件值呢。将该变量添加到*ngIf
此处…如果photo对象或其属性为null或未定义,您将收到一个错误,无法读取null或未定义的calbum,这取决于您需要使用this.owner、this.photo、this.isAdmin的对象的值,请在模板中使用?操作员似乎更轻松了看看我的答案