的角2用法我有一個稱之爲REST端點的服務:的Http觀察的和管
import { Task } from './task';
import { TaskStatus } from './task-status';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class TaskService {
constructor(private http: Http){
}
getTasks() {
return this.http.get('http://localhost:8080/tasks').map(res => res.json()).map(rest => rest._embedded.tasks);
}
}
終點返回這樣的結果:
{
"_embedded": {
"tasks": [
{
"title": "zxc",
"description": "zxc",
"status": "New",
"_links": {
"self": {
"href": "http://localhost:8080/tasks/1"
},
"task": {
"href": "http://localhost:8080/tasks/1"
}
}
},
{
"title": "asd",
"description": "qweqwe",
"status": "New",
"_links": {
"self": {
"href": "http://localhost:8080/tasks/2"
},
"task": {
"href": "http://localhost:8080/tasks/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/tasks"
},
"profile": {
"href": "http://localhost:8080/profile/tasks"
}
},
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
}
我使用的服務在這個組件:
@Component({
selector: 'app-mycomp',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
export class MyComponent implements OnInit {
tasks: Array<Task>;
constructor(private taskService:TaskService) {
taskService.getTasks()
.subscribe(tasks => this.tasks = tasks,
err => console.error(err),
() => console.log('done'));
}
}
模板看起來像這樣;
<task-details *ngFor="let task of tasks" [task]="task"></task-details>
可正常工作,但是當我嘗試在模板中使用管道:
<task-details *ngFor="let task of tasks | WithStatus: TaskStatus.New" [task]="task"></task-details>
我得到了錯誤「無法讀取的未定義的屬性‘過濾器’」。
下面是管道實現:
import { Pipe, PipeTransform } from '@angular/core';
import { TaskStatus } from './task-status';
@Pipe({ name: 'WithStatus', pure: true })
export class TaskStatusFilter implements PipeTransform{
transform(value: any, ...args: any[]): any {
console.log(value);// value is undefined
return value.filter(item => item.status == args[0]);
}
}
'WithStatus:TaskStatus.New'什麼是TaskStatus.New?你在任何地方都沒有這樣的事情...... – micronyks