当angular通过HTTP调用从API接收数据时,如何检查某些属性是否有图像。
答案 0 :(得分:0)
以下是如何以一种非常简单的方式解决这个现实世界的例子。 的 organaization.component.ts 强>
import { Component, OnInit } from '@angular/core';
export class OrganizationComponent implements OnInit {
organizationObj:any = {
logo: 'http://someurl.com/assets/images/'
image: 'http://someurl.com/assets/images/someimg.jpg'
};
imgNoExt:boolean = false;
// here is the simple image extension check function
private urlImgHasExt(url){
return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}
ngOnInit() {
this.imgNoExt = this.urlImgHasExt(this.organizationObj.logo);
console.log(this.imgNoExt) //this will return false value
this.imgNoExt = this.urlImgHasExt(this.organizationObj.image);
console.log(this.imgNoExt) //this will now return true value
}
}