简体   繁体   English

在Angular Typescript中使用数据

[英]Using data in Typescript, Angular

I have a request in Angular 5: 我在Angular 5中有一个要求:

http.get(baseUrl + 'api/Student/Students1')                           
    .subscribe(result => {
        this.std = (result as Students[]);
    }, error => console.error(error));

Interface: 接口:

interface Students {
    Address: string;
    Email: string;
    Phone: string;
    StdName: string;
}

I place data in std , public std : Students[] . 我将数据放在stdpublic std : Students[] How i can turn to certain object? 我怎样才能转向某个物体? (like std[1].Email etc.) (如std[1].Email等)

Help please! 请帮助!

You can do this , this way if you are getting array of Students in response 您可以这样做,如果您得到大量 Students的回应

http.get(baseUrl + 'api/Student/Students1')
.subscribe(result:Students[] => {
    this.std = result;
}, error => console.error(error));

But if you are getting single Student then 但是如果你是单身 Student

http.get(baseUrl + 'api/Student/Students1')
.subscribe(result:Students => {
    this.std.push(result);
}, error => console.error(error));
  1. A class should be singular, not plural 一类应该是单数,而不是复数

     export class Student { ... } 
  2. You should use a class, to instantiate objects, and not an interface. 您应该使用一个类来实例化对象,而不是一个接口。

  3. you don't need to affect a type to your variable like this. 您不需要像这样影响变量的类型。 Declare the type when you declare your variable instead. 在声明变量时声明类型。

     std: Student[]; 
  4. To access properties of your object, you can do this. 要访问对象的属性,可以执行此操作。

     this.std.length; // Array properties this.std[0].Phone; // Properties of the first student 

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

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