2017-02-26 24 views
1

这里有一个简单的角度2类我使用:操纵角2时从服务器返回数据,而不使用管道

public class LogEntry 
{ 
    public int id { get; set; } 
    public string company { get; set; } 
    public string name { get; set; } 
} 

哪些数据是使用.maphttp.get约束:

export class LogService { 

    constructor(@Inject(Http) private http: Http) {} 
    ... 
    return this.http.get(myGlobals.API_URL).map(this.extractData) 
    ... 
} 

private extractData(res: Response) { 
    let body = res.json(); 
    return body.data || { }; 
} 

这里是调用LogService类(log.component.ts)的组件代码:

import { Component, OnInit } from '@angular/core'; 
import { LogEntry } from './log'; 
import { LogService } from './log.service'; 

@Component({ 
    moduleId: module.id, 
    selector: 'log-list', 
    templateUrl: './log-list.component.html', 
    providers: [ LogService ] 
}) 
export class LogListComponent implements OnInit { 
    errorMessage: string; 
    logEntries: LogEntry[]; 
    mode = 'Observable'; 

    constructor (private LogService: LogService) {} 

    ngOnInit() { this.getLog(); } 

    getLog() { 
    this.logService.getLog() 
       .subscribe(
        logs=> this.logEntries = logs, 
        error => this.errorMessage = <any>error); 
    } 

}

这里是我用来显示数据的HTML模板:

<div *ngFor="let logEntry of logEntries" class="row"> 
    <div class="cell">{{logEntry.id}}</div> 
    <div class="cell">{{logEntry.company}}</div> 
    <div class="cell">{{logEntry.name}}</div> 
</div> 

现在比方说,我要处理的数据之前,它显示在HTML。 例如:我想trim()replace()字符串返回logEntry.name - 没有为每个操作创建一个的麻烦。

这可能吗?

谢谢大家。

+0

后,你叫LogService,在那里你指定LogEntries – Sajeetharan

+0

我添加的代码的代码。谢谢。 –

回答

1

您可以使用过滤器,做任何修改,

getLog() { 
    this.logService.getLog() 
       .subscribe(
        logs=> this.logEntries = logs, 
        error => this.errorMessage = <any>error); 
    this.logEntries = this.logEntries.filter(element => { 
      return element.name.trim(); 
     }); 
    } 
+0

我在哪里可以在我发布的代码中使用此代码? –

+0

@KobyDouek更新了答案 – Sajeetharan

+0

我得到:EXCEPTION:无法读取未定义的属性“过滤器” –