在角度2+中如何检查网址是否具有正确的图像扩展名

时间:2017-09-09 06:27:35

标签: javascript angular

当angular通过HTTP调用从API接收数据时,如何检查某些属性是否有图像。

1 个答案:

答案 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
 }
}