1

I'm trying to use json to populate an html page with angular, right now I have this as my JSON:

  [{"listType":"custprof", "prof":"Turkish Government"},
  {"listType":"custprof", "prof":"Eren Isaat"},
  {"listType":"custprof", "prof":"Mardin, Turkey"},
  {"listType":"custprof", "prof":"Canal Irigation channel"},
  {"listType":"situation","sit":"Grade Checking limited to working day"},
  {"listType":"situation", "sit":"5 grade checkers per machine"},
  {"listType":"situation","sit":"Terrain slows accurate grading"},
  {"listType":"situation", "sit":"Fine grading requierd multiple passes"},
  {"finalImage":"img", "foo":"imgname"}] 

and here is my html code

 <div ng-repeat="case in case_select" ng-show="case.listType =='custprof'" >
<ul>
  <li >{{case.prof}}</li>
</ul>
</div>
<h1>Situation</h1>
 <div ng-repeat="case in case_select" ng-show="case.listType== 'situation'">
<ul>
  <li>{{case.sit}}</li>
</ul>
</div>
</div>
</div>

Right now I'm using ng-repeat along with ng-show to get specific strings from the JSON file. I am wondering if there is a better/more direct way of returning a specific result from the JSON, rather then looping through the entire thing and hiding specific items. For instance maybe something that looks like this:

 <div>{{case.finalImage.foo}}</div>
5
  • That is exactly how you would do it... have you tried it out? Commented Feb 3, 2014 at 20:12
  • 3
    Perhaps a filter? Commented Feb 3, 2014 at 20:13
  • Yes, and it works. But I'm wondering if there is a simpler way to achieve the same result. Also the method I have above doesn't ignore the unwanted JSON, it still places them within the html but it sets the display to none, which seems like bad coding to me. @ChristopherMarshall Commented Feb 3, 2014 at 20:15
  • Check out @Blazemonger's suggestion. I have a previous question about filters if you're interested. Commented Feb 3, 2014 at 20:18
  • Filter definitely works better, since it doesn't compile the unwanted JSON like ng-show does. Thanks for the advice! Commented Feb 3, 2014 at 20:24

1 Answer 1

2

Perhaps a filter?

 <div ng-repeat="case in case_select | filter: {listType:custprof}" >
<ul>
  <li >{{case.prof}}</li>
</ul>
</div>
<h1>Situation</h1>
 <div ng-repeat="case in case_select | filter: {listType:situation}">
<ul>
  <li>{{case.sit}}</li>
</ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.