简体   繁体   English

如何从Angular2中的json获取数据

[英]How to get data from json in Angular2

I have a Json response like this. 我有这样的Json回应。

{
    "response": {

        "my_students": {
            "students": [
                {
                    "studentNumber": "123",
                    "studentName": "ABC"

                    "studentaddresse": [
                        {

                            "address": "test"

                        }
                    ]

                },
                 {
                    "studentNumber": "345",
                    "studentName": "CDS"

                    "studentaddresse": [
                        {

                            "address": "test1"

                        }
                    ]

                }
            ]
        }
    }
}

On button click I have to fetch these data. 在按钮上单击我必须获取这些数据。 For that In component.ts file I have this code 对于那个在component.ts文件中,我有以下代码

studdata=[];
 ngOnInit(){
       this.studdata= this.studentService.loadStudents();
}

And in student.service.ts file I have this code student.service.ts文件中,我有此代码

  loadStudents(): any{
      return this.loginService.getStudentData();
   }

And in login.Service.ts file I have this code //On button click i am calling getStudentResponseData() this method In console am getting data.but in getStudentData() method am not getting data login.Service.ts文件中,我有以下代码//单击按钮时,我正在调用getStudentResponseData()此方法在控制台中正在获取数据。但是在getStudentData()方法中未获取数据

 student_data: Object;

     public getStudentResponseData() : Promise<any> {
          if(typeof(this.student_data) === "undefined") {
                return this.http.get('assets/studentapi.json')
                .toPromise().then(res => {

                                      this.student_data = res.json().response;
                       console.log("data"+this.student_data);                
                                      return  this.student_data;
                    }).catch(this.handleError);
          } else {
              return Promise.resolve(this.student_data);
          }
    }
    public getStudentData(): Object {
            return this.student_data;
        }

Can anyone please help me with this,where i am doing wrong? 如果我做错了任何人都可以帮助我吗? And I want to display values in html ,How to display student number here. 我想在html中显示值,如何在此处显示学生编号。

<div *ngFor="let stu of studdata">
<div>{{stu.studentNumber}}</div>
</div>

loadStudents() returns a Promise. loadStudents()返回一个Promise。 So in your component the code has to be like: 因此,在您的组件中,代码必须类似于:

 ngOnInit(){
       this.studentService.loadStudents()
           .then((students) => {
               this.studdata = students;
           });
}

Because this method is async. 因为此方法是异步的。 You can get the data in .Promise().then ( ... you can handle data here ...) 您可以在.Promise().then ( ... you can handle data here ...)获取数据.Promise().then ( ... you can handle data here ...)

You can not get data from getStudentData() function because of that async of method. 由于方法的异步性,您无法从getStudentData()函数获取数据。

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

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