角度6-HTTP从URL获取API

时间:2018-08-07 17:00:26

标签: json angular6 httpmodule

我想从url获取json(url是我用代码编写的来自w3school网站的json代码。)

我关注了this question

我的代码是

app.component.html

argmax

app.component.ts

import numpy as np

A = np.array([[0. , 0. , 0.2, 0.2],
              [0.3, 0. , 0.3, 0. ],
              [0. , 0. , 0. , 0. ]])

res = np.zeros(A.shape)
idx =  np.arange(res.shape[0])
args = A.astype(bool).argmax(1)
res[idx, args] = A[idx, args]

print(res)

array([[ 0. ,  0. ,  0.2,  0. ],
       [ 0.3,  0. ,  0. ,  0. ],
       [ 0. ,  0. ,  0. ,  0. ]])

app.module.ts

<div style="text-align:center">
  <h1>
    {{ title  | uppercase }}
  </h1>
  <table>
    <thead>
      <tr>
        <th>Name</th>
        <th>City</th>
        <th>Country</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let json of Jsons">
        <td>{{ json.Name }}</td>
        <td>{{ json.City }}</td>
        <td>{{ json.Country }}</td>
      </tr>
    </tbody>
  </table>
</div>

json.ts

import { Component , OnInit} from '@angular/core';
import { JsonService } from './json.service';
import { Json } from './json';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'nowian';
  jsons: Json[];

  constructor(private jsonService: JsonService) { }

  ngOnInit() {
    this
      .jsonService
      .getjson()
      .subscribe((data: Json[]) => {
        this.jsons = data;
    });
  }
}

json.service.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { JsonService } from './json.service';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [JsonService],
  bootstrap: [AppComponent]
})
export class AppModule { }

但这不起作用!!!!

0 个答案:

没有答案