2014-03-28 190 views
0

我有一個對象,看起來像這樣:如何從角模板中的鍵獲取對象對象值?

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

我想輸出的鍵值每個對象的名稱。我在for-each中嘗試了{{location.name}},但它不起作用。

+0

首先,您應該使用'ng-href'代替:http://docs.angularjs.org/api/ng/directive/ngHref –

回答

1

首先,你應該使用NG-HREF代替:

採用了棱角分明的標記像{{哈希}}在href屬性將使得 鏈接到錯誤的URL如果用戶點擊它在Angular之前有 機會用其值替換{{hash}}標記。直到角 取代標記鏈接將被打破,最有可能 返回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財產