将Angular 2模型转换为函数

时间:2017-02-07 16:56:20

标签: angular

我从我的firebase数据库中提取数据。

这是我的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;
      }
    });
  }
}
&#13;
&#13;
&#13;

这是我的详细内容:

&#13;
&#13;
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

      });
    });
  }
}
&#13;
&#13;
&#13;

这是我组件中的html。

&#13;
&#13;
<p>{{realEstate.folder}}</p>
&#13;
&#13;
&#13;

这是我得到的数据:

enter image description here

现在,我的问题是。我需要将结果放入&#34;位置&#34;我在photo()函数中有变量。

类似于var location = this.realEstate.folder或{{realEstate.folder}}

我无法弄明白该怎么做。

谢谢

这里是一个插件但当然不会返回任何东西,因为我没有连接到firebase

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

1 个答案:

答案 0 :(得分:0)

我不清楚你要做什么,但也许这可能让你更接近?

请注意,this.location将是'undefined',直到您的observable返回数据。如果您在模板中使用它,则可以在标签上使用* 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;
          });
        });
      }
    }