我正在編寫一個應用程序,要求用戶點擊某個位置,然後通過Google地圖將其引導至該位置。我有數千個以JSON格式存儲的位置。我希望能夠點擊位置的名稱,並讓應用程序從文件中拉出座標。到目前爲止我有這個代碼。從JSON文件中獲取特定信息
HTML
<body>
<h1>ECC Site Lookup</h1>
<div ng-controller="firstCtrl">
<input type="text" ng-model="search" border="1" />
<ul>
<li ng-repeat="site in SiteLocs |filter : search ">
<a href="http://maps.google.com/?q=" + {{site.Point.coordinates}}> {{site.name}} </a>
</li>"
<li ng-repeat="site in SiteLocs |filter : search "></li>
</ul>
</div>
</body>
我覺得有什麼不對,這是HREF部分。具體的{{site.Point.coordinates}}
這裏是我的JS代碼
(function() {
var app = angular.module('app', []);
app.controller('firstCtrl', function($scope) {
$scope.SiteLocs = [{
"name": "502 Nelson St, Greenville, MS 38701",
"visibility": "0",
"description": "502 Nelson St, Greenville, MS 38701",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-91.05636,33.415485,0"
}
}, {
"name": "242 Blackhawk Trace, Galena, IL 61036",
"visibility": "0",
"description": "242 Blackhawk Trace, Galena, IL 61036",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-90.319778,42.390862,0"
}
}, {
"name": "3747 Ocean Dr, Vero Beach, FL 32963",
"visibility": "0",
"description": "3747 Ocean Dr, Vero Beach, FL 32963",
"styleUrl": "#waypoint",
"Point": {
"coordinates": "-80.358248,27.659094,0"
}];
angular.forEach($scope.SiteLocs, function(location){
// if last 2 characters in string are ",0"
var clength = location.Point.coordinates.length;
if (location.Point.coordinates.substring(clength-2,clength)===",0")
{
location.Point.coordinates = location.Point.coordinates.substring(0,clength-2);
}
});
});
}());
這是部分錯誤。 [不要將{href'與{{}}但使用ng-href](https://docs.angularjs.org/api/ng/directive/ngHref)一起使用。不要在括號中包含URI! – Pak
謝謝,解決了不加載鏈接的問題。但我想弄清楚如何讓它加載一個座標。因爲{{site.Point。座標}}是幾千個不同的座標。 –
@Pak它在'ng-repeat'中不是問題,因爲直到它被編譯,元素才能存在 – charlietfl