Javascript 角度贴图总是返回未定义的

Javascript 角度贴图总是返回未定义的,javascript,angular,Javascript,Angular,我有一个有角度的页面,其中有一个属性列表。每个属性都有一个用于查看属性详细信息的链接。我有一个按id映射属性的函数,但是作为返回,我总是得到未定义(在控制台中打印) 服务.ts getProperties(): Observable<IProperty[]>{ return this.http.get<IProperty>('http://localhost:3000/') .do(data => this.properties.push(JS

我有一个有角度的页面,其中有一个属性列表。每个属性都有一个用于查看属性详细信息的链接。我有一个按id映射属性的函数,但是作为返回,我总是得到未定义(在控制台中打印)

服务.ts

 getProperties(): Observable<IProperty[]>{
     return this.http.get<IProperty>('http://localhost:3000/')
     .do(data => this.properties.push(JSON.stringify(data)))
     .catch(this.handleError);  
 }

 getProperty(id:number): Observable<IProperty>{
    return this.getProperties()
    .map((properties: IProperty[])=> properties.find(p=>p.propertyId===id));
}
import { Component, OnInit } from '@angular/core';
import { IProperty } from './property';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiService} from '../api/api.service';
import { PropertyGuardService } from './property-guard.service' 


@Component
({
  selector: 'propertyDetail',
  templateUrl: './propertyDetail.component.html',
  providers: [PropertyGuardService]
})

export class PropertyDetailComponent implements OnInit
{

    pageTitle:string = "Property Details";
    property: IProperty;
    errorMessage: string ="";

    constructor(private _route : ActivatedRoute,
                private _router: Router,
                private apiService: ApiService){
       console.log(this._route.snapshot.paramMap.get('id'));
    }

    ngOnInit(){
        const param = this._route.snapshot.paramMap.get('id');
        if(param){
            const id = +param;
            this.getProperty(id);

            console.log(this.getProperty(id));
        }            
    }

    getProperty(id:number)
    {
            this.apiService.getProperty(id).subscribe(
            property => this.property = property,
            error => this.errorMessage = <any>error);
    }

    onBack(): void
    {
        this._router.navigate(['/properties']);
    }
}  
getProperties():可观察{
返回此.http.get('http://localhost:3000/')
.do(data=>this.properties.push(JSON.stringify(data)))
.接住(这个.把手错误);
}
getProperty(id:number):可观察{
返回此参数。getProperties()
.map((properties:IProperty[])=>properties.find(p=>p.propertyId==id));
}
propertydeail.component.ts

 getProperties(): Observable<IProperty[]>{
     return this.http.get<IProperty>('http://localhost:3000/')
     .do(data => this.properties.push(JSON.stringify(data)))
     .catch(this.handleError);  
 }

 getProperty(id:number): Observable<IProperty>{
    return this.getProperties()
    .map((properties: IProperty[])=> properties.find(p=>p.propertyId===id));
}
import { Component, OnInit } from '@angular/core';
import { IProperty } from './property';
import { ActivatedRoute, Router } from '@angular/router';
import { ApiService} from '../api/api.service';
import { PropertyGuardService } from './property-guard.service' 


@Component
({
  selector: 'propertyDetail',
  templateUrl: './propertyDetail.component.html',
  providers: [PropertyGuardService]
})

export class PropertyDetailComponent implements OnInit
{

    pageTitle:string = "Property Details";
    property: IProperty;
    errorMessage: string ="";

    constructor(private _route : ActivatedRoute,
                private _router: Router,
                private apiService: ApiService){
       console.log(this._route.snapshot.paramMap.get('id'));
    }

    ngOnInit(){
        const param = this._route.snapshot.paramMap.get('id');
        if(param){
            const id = +param;
            this.getProperty(id);

            console.log(this.getProperty(id));
        }            
    }

    getProperty(id:number)
    {
            this.apiService.getProperty(id).subscribe(
            property => this.property = property,
            error => this.errorMessage = <any>error);
    }

    onBack(): void
    {
        this._router.navigate(['/properties']);
    }
}  
从'@angular/core'导入{Component,OnInit};
从“./property”导入{IProperty};
从'@angular/Router'导入{ActivatedRoute,Router};
从“../api/api.service”导入{ApiService};
从“./property guard.service”导入{PropertyGuardService}
@组成部分
({
选择器:“propertyDetail”,
templateUrl:'./propertyDetail.component.html',
提供者:[PropertyGuardService]
})
导出类PropertyDetailComponent实现OnInit
{
pageTitle:string=“属性详细信息”;
财产:知识产权;
errorMessage:string=“”;
构造函数(专用路由:ActivatedRoute,
专用路由器:路由器,
私人apiService:apiService){
log(this._route.snapshot.paramMap.get('id');
}
恩戈尼尼特(){
const param=this._route.snapshot.paramMap.get('id');
如果(参数){
常量id=+param;
这个.getProperty(id);
log(this.getProperty(id));
}            
}
getProperty(id:number)
{
此.apiService.getProperty(id).subscribe(
property=>this.property=property,
error=>this.errorMessage=error);
}
onBack():void
{
此.u router.navigate(['/properties']);
}
}  

您可以通过在适当的时间订阅来使用
property=>this.property=property
设置一个属性,现在您可以在视图中使用
this.property
,如
{property?.id}

如果要检查
属性
是否在控制台上正确获取,可以使用以下方法:

getProperty(id:number){
   this.apiService.getProperty(id).subscribe(
      property => {
        this.property = property,
        console.log(property) // check property returned here           
      });
}

getProperties
看起来像什么?@echonax添加了代码,基本上它调用服务器来获取mongoose集合中的所有数据。它打印undefined,因为您返回undefined。正如预期的那样。@dfsq为什么预期它是未定义的?这是预期的,因为
getProperty
返回(隐式)
undefined
。你还了什么吗?不,所以它是未定义的。