JSON
객체 배열을 필터링하는 파이프를 작성하려고 합니다. 모든 객체에는 부울 ( demo
,) github
인 3 개의 키가 있으며 , 이 키를 finished
필터에 입력하고 키가 참인 객체 만 표시하고 싶습니다. 여러 값을 입력 할 필요가 없으며 단일 문자열 (키)이면 충분합니다.
지금까지 필터에 입력 한 내용에 상관없이 페이지에 데이터가 표시되지 않습니다. 필터를 완전히 제거하면 서비스에 모든 것이 정의됩니다. 기록 된 오류 메시지도 없습니다.
그래서 페이지를 제공하는 서비스가 있습니다.
import { Injectable } from 'angular2/core';
export class Page {
constructor(public img: string, public name: string, public repo: string, public description: string, public demo: boolean, public github: boolean, public finished: boolean) { }
}
@Injectable()
export class PagesService {
getPages() {
return [
new Page('./app/images/placeholder.png', 'veryNiceWords', 'https://github.com/Shooshte/veryNiceWords', 'A hobby app, made to enable posting, rating and sharing quotes over social networks. Work in progress.', false, true, false),
new Page('./app/images/placeholder.png', 'ZIC IJS', 'https://github.com/Shooshte/ZIC', 'Refurbishing of on old library webpage with AngularJS.', false, true, false),
new Page('./app/images/weather.png', 'Show the Local weather', 'http://codepen.io/shooshte/pen/NxOwOX', 'A freeCodeCamp exercise, designed to show the local weather.', true, false, true),
new Page('./app/images/calculator.png', 'Calculator', 'http://codepen.io/shooshte/pen/qbjJdy', 'A freeCodeCamp exercise, which requires you to build a javascript calculator.', true, false, true),
new Page('./app/images/github.png', 'MTGO Draft Replayer', 'https://github.com/Shooshte/MTGO-Draft-Replayer', 'A simple web app that opens a MTGO draft log file, and re-creates the draft from it.', false, true, false),
new Page('./app/images/codeeval.png', 'codeEval', 'https://github.com/Shooshte/CodeEval', 'CodeEval challenges solutions written in javascript and posted to gitHub.', false, true, true)
];
}
}
여기에서 OnInit 서비스를 호출하고 파이프를 정의합니다.
import { Component } from 'angular2/core';
import { ViewEncapsulation } from 'angular2/core';
import { Page, PagesService } from './pages.service';
import { Pipe, PipeTransform } from 'angular2/core';
@Pipe({ name: 'pagesFilter' })
export class pagesFilter {
transform(pages, [key]) {
return pages.filter(page => {
return page.key === true;
});
}
}
@Component({
selector: 'portfolio',
templateUrl: '/app/views/portfolio.html',
styleUrls: ['../app/styles/PortfolioMobile.css', '../app/styles/PortfolioOther.css'],
pipes: [pagesFilter],
encapsulation: ViewEncapsulation.None
})
export class PortfolioComponent {
filter = 'everything';
pages: Page[];
constructor(private _pagesService: PagesService) { }
ngOnInit() {
this.pages = this._pagesService.getPages();
}
}
이것은 내 HTML에서 파이프를 사용하는 방법입니다.
<div class="portfolioContainer">
<div class="displayHack"></div>
<div *ngFor="#p of pages | pagesFilter:demo" class="portfolioPageContainer">
<img [attr.src]="p.img" class="portfolioThumbnail">
<h2>{{ p.name }}</h2>
<a [attr.href]="p.repo">
<div>
<p>{{ p.description }}</p>
</div>
<p class="portfolioRepoLink">See the Code!</p>
</a>
</div>
<div class="displayHack"></div>
</div>
대신 이것을 시도 할 수 있습니다.
@Pipe({ name: 'pagesFilter' })
export class pagesFilter {
transform(pages, [key]) {
return pages.filter(page => {
return page[key] === true; // <------
});
}
}
귀하의 경우에는 키 매개 변수의 내용에 해당하는 이름이 아닌 "key"라는 이름으로 속성에 액세스하려고합니다.
또한 "demo"값을 사용하려면 ( "demo"표현식을 평가하지 않음) 다음을 사용해야합니다.
<div *ngFor="#p of pages | pagesFilter:'demo'"
class="portfolioPageContainer">
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다