简体   繁体   English

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

[英]Display data from Angularjs2 file(.ts) in html page

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. 请帮助我解决此错误。

use "?" 采用 ”?” notation in your HTML , if profileObj exists only then it will search for statusCode HTML中的表示法,如果profileObj仅存在,则它将搜索statusCode

{{profileObj?.statusCode}} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM