【发布时间】:2019-02-22 04:21:33
【问题描述】:
我目前正在尝试过滤从硬编码数据返回的 JSON。在我的组件中,我的代码目前如下所示:
export class CtaComponent implements OnInit {
@Input() ctaId: string;
public ctaCards: CtaCards.ICtaCard[];
constructor(private ctaService: CtaService) {
}
ngOnInit() {
if (this.ctaId !== undefined) {
this.ctaService.getCtaCards(this.ctaId)
.subscribe(x => {
this.ctaCards.find(z => z.id == this.ctaId) = x
})
}
}
}
我相信您可以看到,find 行不起作用。 ctaId 是用于过滤 JSON id 的 id。然而,find 的代码被突出显示为一个错误,并说The left hand-side assignment expression must be a variable or property access. 我对 Angular 还是很陌生,所以这可能只是一些明显的错误。我的研究似乎对我没有任何帮助。
我的服务如下所示:
export class CtaService {
private ctaCard: BehaviorSubject<CtaCards.ICtaCard[]>
private productUrl = 'src/assets/cta.json'
constructor(private http: HttpClient) {}
public getCtaCards(id: string): BehaviorSubject<CtaCards.ICtaCard[]{
if (id) {
this.ctaCard = new BehaviorSubject<CtaCards.ICtaCard[]>(null);
this.http.get(this.productUrl).pipe(
retry(3)
)
.subscribe((x: CtaCards.ICtaCard[]) => this.ctaCard.next(x))
}
return this.ctaCard;
}
}
【问题讨论】:
-
这里的
z是什么? -
@HarunurRashid 它是
Cta card,所以在这种情况下,它会得到Cta Card的id。 -
@FrankFajardo 它稍后会使用它,一开始的检查只是为了检查。
-
这段代码没有意义:
this.ctaCards.find(z => z.id == this.ctaId) = x。我想你的意思是this.ctaCards.find(z => z.id == this.ctaId) == x或this.ctaCards.find(z => z.id == this.ctaId) === x。但话又说回来,这是一个合乎逻辑的检查。之后会发生什么? -
@FrankFajardo 你是对的,我会去重新评估我的代码。