[英]Angular 2 - Unable to assign return value from http subscribe() method to a global variable inside component
我在angular 2中使用http.get获取一个JSON文件。
最近服务:
import { Http, Response } from '@angular/http';
//import { Observable } from '../../../../../../node_modules/rxjs/Observable';
import { Observable } from '../../../../../../node_modules/rxjs/Rx';
import { Injectable } from '@angular/core';
@Injectable()
export class RecentService {
private _url = 'http://localhost:3001/api/uploadedFiles';
constructor(private _http: Http) {
}
getJson() {
return this._http.get(this._url)
.map(res => res.json());
}
}
最近的component.ts:
import {Component, OnInit} from '@angular/core';
import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass, NgStyle} from '@angular/common';
import {RecentService} from './recent.service';
import {HTTP_PROVIDERS} from '@angular/http';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'recent',
pipes: [],
providers: [RecentService, HTTP_PROVIDERS],
styleUrls: ['/app/pages/content/components/recent.styles.css'], //might need to change reference
template: require('./recent.html') // `<strong>My edit page</strong>`
})
export class Recent implements OnInit{
allFiles;
public allFiles_error:Boolean = false;
openModalWindow:boolean=false;
images = [];
currentImageIndex:number;
opened:boolean=false;
imgSrc:string;
constructor(private _recentService: RecentService) {
this._recentService.getJson()
.subscribe(data => {
this.allFiles = data;
console.log("allFiles: ", this.allFiles);
console.log(this.allFiles.length);
for(var i = 0; i < this.allFiles.length; i++) {
this.images.push({
thumb: this.allFiles[i].url,
img: this.allFiles[i].url,
description: "Image " + (i+1)
});
}
console.log("Images: ", this.images);
},
err => { this.allFiles_error = true },
() => console.log('Completed!')
);
}
ngOnInit() {
console.log(this.images.length);
console.log(this.images);
console.log(typeof this.images);
}
//this is the function where I want to access this.images
openGallery(index) {
if(!index) {
this.currentImageIndex = 1;
}
this.currentImageIndex = index;
this.opened = true;
for (var i = 0; i < this.images.length; i++) {
if (i === this.currentImageIndex ) {
this.imgSrc = this.images[i].img;
break;
}
}
console.log(this.imgSrc);
}
this.allFiles是JSON对象的数组。 我试图将选定的数据从this.allFiles存储到this.images。 我还想在整个组件中将this.images作为全局变量访问,但由于使用http.get()进行异步调用而无法访问。 我在HTML中尝试了异步管道,但这也导致了错误。 有没有办法全局访问this.images?
提前致谢。
我对TypeScript和Angular2的经验很少,但是我想您的问题是箭头功能和this
关键字。 每当在错误函数上使用方括号时,其内部的this
便开始引用该函数本身,而不是所需的上层类。 还要对错误进行同样的处理。
还要从构造函数中删除该调用,并ngOnInit
使用ngOnInit
。
请尝试这样的操作,并让我知道:
export class Recent implements OnInit{
allFiles;
public allFiles_error:Boolean = false;
openModalWindow:boolean=false;
images = [];
currentImageIndex:number;
opened:boolean=false;
imgSrc:string;
constructor(private _recentService: RecentService) { }
ngOnInit() {
this._recentService.getJson().subscribe(
data => initData(data),
err => handleError(),
() => console.log('Completed!'));
}
initData(data){
this.allFiles = data;
console.log("allFiles: ", this.allFiles);
console.log(this.allFiles.length);
for(var i = 0; i < this.allFiles.length; i++) {
this.images.push({
thumb: this.allFiles[i].url,
img: this.allFiles[i].url,
description: "Image " + (i+1)
});
}
console.log("Images: ", this.images);
console.log(this.images.length);
console.log(this.images);
console.log(typeof this.images);
}
handleError() {
this.allFiles_error = true;
}
}
您想在哪里访问this.allFiles ?
您可以将其添加到模板中, 而无需任何管道,因为它位于Observables的订阅内部,Angular2知道可以重新渲染模板,并且它将被更新。
如果您需要从组件访问其他函数,请在.subscribe函数内部添加函数调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.