简体   繁体   English

Angular 5-JSON到“范围”绑定到* NgFor

[英]Angular 5 - JSON to “scope” binding to *NgFor

I want to bind the data that i get from my JSON request to the *NgFor to be able to read it out, This is how the data looks like that i get in: 我想将我从JSON请求中获得的数据绑定到* NgFor以便能够读出它,这就是我获得的数据的样子:

{Id: null, Code: "9348892084", HasInfo: true, Info: Array(26)}
HasInfo:true
Info:
Array(26)
0:{ProductId: 32631, Name: "JNOOS", Image: "http://sd-m-mg", …}
1:{ProductId: 32969, Name: "SWIFT", Image: "http://sd-33087.jpg",…}
2:{ProductId: 32570, Name: "GABIX", Image: "http://sd-c7273.jpg", …}
3:{ProductId: 32473, Name: "MISMA", Image: "http://sd-mt8-8343e4d95.jpg", …}

I was working with AngularJS before and there i made the request as such: 我之前与AngularJS一起工作,因此我提出了这样的要求:

$scope.getAll{

    $http({
        method: 'Get',
        url: "http://URL/" + Code
        })
        .success(function (data) {
           $scope.All = data.Info;
        })

No i am moving to Angular5 and i would like to get the same array of information bind to the : 不,我要移至Angular5,我想将相同的信息数组绑定到:

<div *ngFor="let x of info">
    {{ x.Name }}
</div>

How would i adjust the below to get the same as above? 我将如何调整以下内容以使其与以上相同?

export class AppComponent {

  readonly ROOT_URL = 'http://url/content/'
  constructor(private http: HttpClient) {}

ngOnInit() {
  this.getData()
}

getData() {
  this.http.get(this.ROOT_URL + 'Getscope')
    .subscribe(
      data => {
        var test = data;   
        // var info = data.Info = not valid!;  
        // var test can't be approached by *ngFor
        console.log(test);
        console.log(test.info);

        //$scope.info = data.Info;
        //this.info = data;
      }, error => {
        console.error("HTTP FAILURE ERROR!!!");
      }
    )
 };
}

Also the json output has an array inside an object, do i say this correct? 另外json输出在对象内部有一个数组,我说这个正确吗?

From your code you are using info in html but not assigning to any class variable , 从您的代码中,您正在使用html info但未分配给任何类变量

Take a public variable public info; 获取一个公共变量的public info; and assign data using this.info = data.Info 并使用this.info = data.Info分配数据

Component: 零件:

export class AppComponent {

  readonly ROOT_URL = 'http://url/content/'
  public info;
  constructor(private http: HttpClient) {}

ngOnInit() {
  this.getData()
}

getData() {
  this.http.get(this.ROOT_URL + 'Getscope')
    .subscribe(
      data => {
        this.info = data['Info']; // here is the change
      }, error => {
        console.error("HTTP FAILURE ERROR!!!");
      }
    )
 };
}

Your HTML can be same: 您的HTML可以相同:

<div *ngFor="let x of info">
    {{ x.Name }}
</div>

The simplest solution would be to use an async pipe. 最简单的解决方案是使用异步管道。 This way you do not need to subscribe to the stream returned by http.get . 这样,您无需订阅http.get返回的流。

app.component.ts app.component.ts

import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';

export class AppComponent {
  readonly url = 'http://url/content/'

  constructor(private http: HttpClient) {}

  info$: Observable<any[]>;

  ngOnInit() {
    this.info$ = this.http.get(this.url + 'Getscope')
      .pipe(map(resp => resp.Info));
  }

}

app.component.html app.component.html

<div *ngFor="let x of info$ | async">
    {{ x.Name }}
</div>

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

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