我似乎無法設法使用來自json文件的信息製作視圖的變量,但我非常接近。我可以回顯.subscribe()
鏈中的信息,但它不會將其設置爲變量,他們只是不確定,我做錯了什麼?如何正確提取Angular 2中的http.get中的JSON數據?
我只想將我的json文件加載到組件視圖中的變量中。這是很容易在角1
我的服務:
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Rx';
@Injectable()
export class GullkornService {
result: any;
constructor(private http:Http) {
}
getGullkorn() {
let result = this.result;
this.http.get('./gk/gullkorn.json')
.map(res => res.json())
.subscribe(
val => this.result = val,
err => console.error(err),
() => console.log(this.result)); // this one echoes out what i want
console.log(this.result); // but this one doesnt, so i cant return it
}
}
而且登陸組件:
import { Component, OnInit } from '@angular/core';
import {Router, RouterOutlet} from '@angular/router';
import { HttpModule, JsonpModule } from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { GullkornService } from '../../gullkorn-service.service';
import { FormsModule } from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import "gsap";
declare var ease, TimelineMax,TweenMax,Power4,Power1,Power2,Power3,Bounce, Elastic:any;
@Component({
selector: 'gullkorn-visning',
providers: [GullkornService],
templateUrl: './landing.component.html',
styleUrls: ['./landing.component.css']
})
export class LandingComponent implements OnInit {
gullkorn: any;
constructor(GullkornService: GullkornService) {
this.gullkorn = GullkornService.getGullkorn();
console.log(this.gullkorn);
}
ngOnInit() {
}
}
基於當前的代碼,這是我所得到的:
我有這個項目:github。
謝謝!這是第一次嘗試,對我來說這是一場鬥爭。欣賞你的時間很多。使用組件中的訂閱調用函數有訣竅! –
不客氣!是的,我明白了。異步操作在開始時非常混亂!在我的回答中,還增加了更多的上下文。g如果您需要在導航到頁面時以某種方式操作組件中的數據,則需要注意訂閱中哪些數據可用,哪些不可用,認爲它對未來有用:)快速編碼! :) – Alex