如何从角度模板中的键获取对象对象值?

时间:2014-03-28 13:56:02

标签: angularjs

我有一个看起来像这样的对象:

{
    Kanaanbadet: {"desc":"Badplats med två stränder. Stor gräsyta. Hopptorn och utomhusdusch. Handikapptoalett finns. Välbesökt bad. Badet är bemannat med fritidsledare måndag till söndag kl 11 – 17 under sommarlovet. Stor parkeringsplats finns på gångavstånd, cirka 5 minuters promenad.","name":"Kanaanbadet","image":"http://www.stockholm.se/Web/Core/Pages/Special/StreamServiceGuideImage.aspx?path=%2fWeb%2fCore%2fPages%2fSpecial%2fServiceGuideFile.aspx%3ffileid%3d14b313cb2b2f45e380eb88156c95b539","_cached_page_id":"4b71e342c82be9de1c74de3c2f57ea1c4dde8150","long":"17.85448","lat":"59.34966","url":"http://www.stockholm.se/-/Serviceenhetsdetaljer/?enhet=cf0a856830e4422cb55dcd60e8e6b40b"},
    Johannelundsbadet:{"desc":"Liten badplats med liten sandstrand och stor gräsyta. Gungdjur, parksoffor och bord, torrklosett.","name":"Johannelundsbadet","image":"http://www.stockholm.se/Web/Core/Pages/Special/StreamServiceGuideImage.aspx?path=%2fWeb%2fCore%2fPages%2fSpecial%2fServiceGuideFile.aspx%3ffileid%3d3e4c2056b5534cfc9b0799e2377b8ce4","_cached_page_id":"18cf34222d74612979afd945a925abc0bf16e44d","long":"17.98914","lat":"59.34098","url":"http://www.stockholm.se/-/Serviceenhetsdetaljer/?enhet=ebf8d49780224e908064551c35dbcca4"},
}

在模板中,我可以成功输出密钥,每个密钥都是这样的:

<a href="#/{{location}}" class="location" ng-repeat="(location, details) in locations">{{location}}</a>

我想从每个objcect的名称输出键值。我在for-each中尝试了{{location.name}},但它不起作用。

1 个答案:

答案 0 :(得分:1)

首先,你应该使用ng-href:

  

在href属性中使用像{{hash}}这样的Angular标记将会成为   如果用户在Angular拥有之前单击它,则链接将转到错误的URL   有机会用它的值替换{{hash}}标记。直到Angular   替换标记链接将被破坏并且很可能   返回404错误。

来自angular documentation

尝试使用{{details.name}}代替{{location.name}}

<a ng-href="#/{{location}}" class="location" ng-repeat="(location, details) in locations">{{location}} {{details.name}} </a>
  • location是你的钥匙:“Kanaanbadet”和“Johannelundsbadet”
  • details是具有name属性
  • 的各个键的值