简体   繁体   中英

Angular.js - How do I check to see if a string is contained within an array object using a filter?

I'm using Angular and Firebase to create an app that will allow users to post new topics for discussion, as well as vote on existing topics.

When a user is logged in their username is stored in currentUser.username . If they've upvoted a topic than their username also appears in topic.upvotes .

The problem I'm running into is topic.upvotes is storing an array of values. So if Kenneth and Tyler have both upvoted a topic, topic.upvotes = {"Kenneth":"Kenneth","Tyler":"Tyler"} .

All I need to know is if a user has upvoted a topic so that I can apply some conditional CSS to give them visual feedback that they've already voted on that topic.

I've been trying to accomplish this with:

ng-class="{user_has_upvoted : signedIn() && topic.upvotes === currentUser.username}"

My intent is to apply the user_has_upvoted class to the div only if the user is both signed in and they've actually upvoted the topic.

But obviously topic.upvotes (an array) doesn't == or === currentUser.username (a string).

I was hoping there was some way of using some sort of inArry() function, but angular doesn't have anything that I've found that will work.

Any advice? Thanks!

用这个:

ng-class="{user_has_upvoted : signedIn() && topic.upvotes[currentUser.username].localeCompare(currentUser.username) === 0}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM