【问题标题】:Calling single result from JSON with angular使用角度从 JSON 调用单个结果
【发布时间】:2014-02-03 20:08:52
【问题描述】:

我正在尝试使用 json 来填充带有 angular 的 html 页面,现在我将它作为我的 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"}] 

这是我的html代码

 <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>

现在我正在使用 ng-repeat 和 ng-show 从 JSON 文件中获取特定的字符串。我想知道是否有更好/更直接的方法从 JSON 返回特定结果,而不是循环遍历整个事物并隐藏特定项目。例如,可能看起来像这样:

 <div>{{case.finalImage.foo}}</div>

【问题讨论】:

  • 这正是你会做的......你试过了吗?
  • 可能是filter
  • 是的,它有效。但我想知道是否有更简单的方法来实现相同的结果。此外,我上面的方法并没有忽略不需要的 JSON,它仍然将它们放在 html 中,但它将显示设置为无,这对我来说似乎是糟糕的编码。 @ChristopherMarshall
  • 查看@Blazemonger 的建议。如果您有兴趣,我之前有一个关于过滤器的问题。
  • Filter 肯定效果更好,因为它不像 ng-show 那样编译不需要的 JSON。感谢您的建议!

标签: javascript jquery html json angularjs


【解决方案1】:

也许是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>

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多