2017-02-07 5 views
0

私はfirebaseデータベースからデータをプルします。関数への角2モデル

これは、これは私の詳細コンポーネントである私のservice.ts

import { Injectable } from '@angular/core'; 
 
import { Observable } from 'rxjs/Observable'; 
 
import {Http, Response, RequestOptions} from '@angular/http'; 
 
import { RealEstatePhotography } from './real-estate-photography'; 
 
import {AngularFire, FirebaseListObservable} from 'angularfire2'; 
 
import 'rxjs/add/operator/map'; 
 

 
@Injectable() 
 
export class RealEstatePhotographyService { 
 

 
    constructor(private af: AngularFire, private http: Http) { } 
 

 
private realEstate = this.af.database.list('realestate'); 
 

 
    getItems(){ 
 
    return (this.realEstate); 
 
    } 
 

 

 
    getItem(title: RealEstatePhotography) : Observable<any>{ 
 
    return this.realEstate 
 
    .map((list: Array<any>) => { 
 
     let result: RealEstatePhotography = new RealEstatePhotography(); 
 
     if (list){ 
 
     list.forEach(element => { 
 
      if (element.title === title){ 
 
      result = element 
 
      } 
 
     }); 
 
     return result; 
 
     } 
 
    }); 
 
    } 
 
}

です:

import { Component, OnInit } from '@angular/core'; 
 
import { DomSanitizer, SafeResourceUrl, SafeUrl, SafeHtml } from '@angular/platform-browser'; 
 
import { ActivatedRoute } from '@angular/router'; 
 
import { RealEstatePhotography } from '../real-estate-photography'; 
 
import { RealEstatePhotographyService } from '../real-estate-photography.service'; 
 

 
@Component({ 
 
    selector: 'app-real-estate-photography-detail', 
 
    templateUrl: './real-estate-photography-detail.component.html', 
 
    styleUrls: ['./real-estate-photography-detail.component.css'], 
 
    providers: [RealEstatePhotographyService], 
 
}) 
 
export class RealEstatePhotographyDetailComponent implements OnInit { 
 

 
photos = this.photo(); 
 
realEstate; 
 

 

 
    constructor(private route: ActivatedRoute, private _realEstatePhotographyService: RealEstatePhotographyService, private sanitizer: DomSanitizer) { } 
 

 
    photo(){ 
 
    var location = "" 
 
    for (var i = 0; i < location.length; i++){ 
 
     Array(i); 
 
    } 
 
    return console.log(Array(i)); 
 

 
    } 
 

 

 
    ngOnInit(){ 
 
    this.route.params.subscribe(params => { 
 
     let title = params['title']; 
 
     let realEstate = this._realEstatePhotographyService.getItem(title).subscribe(realEstate => { 
 
     this.realEstate = realEstate 
 

 
     }); 
 
    }); 
 
    } 
 
}

これはトンから私のhtmlですハットコンポーネント。

<p>{{realEstate.folder}}</p>

これは私が取得データである:

enter image description here

さて、私の質問です。私は、その結果をphoto()関数の "location"変数に入れる必要があります。私はそれを行う方法を見つけ出すことはできませんVAR場所= this.realEstate.folderまたは{{}} realEstate.folder

よう

何か。

はここ

がplunkですが、もちろん、私は

https://plnkr.co/edit/a0WKevpXpwvY8x7NG1cb?p=preview

+0

プランナーを提供しますか? –

+0

私は問題を理解していません: "私はその結果を '場所'変数に入れる必要があります。あなたが達成しようとしていることを説明してください。 – Per

+0

Per、私はちょっと混乱していることを知っています。説明するのは本当に難しいです。 私はページに結果が表示されますか?まあ、そのデータを場所 に置く必要があります:var location = "" https://s3.amazonaws.com/gadaphotos/photos/portfolio/alden_park/ " しかし、私のfirebaseからそのデータを取得します。 {{realEstate.folder}}を入れて、そのデータを取得します。同じデータを取得するために、var location = {{realEstate.folder}}のようなものを置いておきたいです。ありがとう –

答えて

0

をfirebaseに接続していないですので、私はあなたが何であるかに完全に明確ではないよ何に戻らないありがとうおそらくこれはあなたを近づけるかもしれませんか?

observableがデータを返すまでthis.locationは '未定義'になることに注意してください。テンプレートでこれを使用している場合は、タグで* ngIf = "location"を使用できます。

export class RealEstatePhotographyDetailComponent implements OnInit { 

    photos = this.photo(); 
    realEstate; 
    private location: any; 

     constructor(private route: ActivatedRoute, private _realEstatePhotographyService: RealEstatePhotographyService, private sanitizer: DomSanitizer) { } 

     photo(){ 

if(this.location) { 


     for (var i = 0; i < this.location.length; i++){ 
      Array(i); 
     } 
     return console.log(Array(i)); 
    } 
     } 


     ngOnInit(){ 
     this.route.params.subscribe(params => { 
      let title = params['title']; 
      let realEstate = this._realEstatePhotographyService.getItem(title).subscribe(realEstate => { 
      this.realEstate = realEstate 
    this.location=realEstate.folder; 
      }); 
     }); 
     } 
    } 
+0

ありがとう、それは動作しません。別の代替案を探しています。最終的にデータを得ることができますが、それでも、それは私の元気なことをしませんd。 ご協力いただきありがとうございます –

関連する問題