在html页面中显示Angularjs2文件(.ts)中的数据

时间:2022-08-24 16:58:16

code in my .ts file is

我的.ts文件中的代码是

  constructor(
      private config  :  ConfigService,
      private http: Http){ 
      this.getWS('ngoName')
      .do((data) => {
        console.log(data);
        this.saveObj(data);
      }).toPromise();
}

saveObj(data:any){
  this.profileObj = data;
  console.log(this.profileObj);    
}   

  getWS(ngoName:string):any{  
    return this.http.get(this.config.serverBaseURL + "ngos.php?ngoId=1")
        .map((res: Response) => res.json());            
  }

the object which i have to use in my html file is the profileObj, structure of profileObj is like this

我必须在我的html文件中使用的对象是profileObj,profileObj的结构是这样的

{
  "statusCode"   : "200",
   "message"     : "success",
   "ngoDetails"  : [
{"ngoId":"1","ngoName":"Test 1","emailId":"ritesh@gmail.com"}]}

but when i am giving {{profileObj.statusCode}} i am getting error:

但是当我给{{profileObj.statusCode}}时,我收到错误:

zone.js:357 Error: Uncaught (in promise): Error: Error in ./Profile class Profile - inline template:28:9 caused by: Cannot read property 'statusCode' of undefined

zone.js:357错误:未捕获(在承诺中):错误:./Profile类中的错误配置文件 - 内联模板:28:9导致:无法读取未定义的属性'statusCode'

please help me to resolve this error.

请帮我解决这个错误。

1 个解决方案

#1


2  

use "?" notation in your HTML , if profileObj exists only then it will search for statusCode

使用 ”?” HTML中的表示法,如果profileObj只存在,那么它将搜索statusCode

{{profileObj?.statusCode}} 

#1


2  

use "?" notation in your HTML , if profileObj exists only then it will search for statusCode

使用 ”?” HTML中的表示法,如果profileObj只存在,那么它将搜索statusCode

{{profileObj?.statusCode}}