を使用してFirebaseからデータを返すときに、私はFirebaseからデータを取得していると私はこのようにJSONに変換するとき:JSON変換問題角度
["{ "lat": "50", "lon": "3", "title": "my title", "content": "my content" }"]
私はどのように変換します:
$scope.array.push(angular.toJson(obj));
私はこれを取得私はこれを取得するように、それは:私の角度指令で
[{ "lat": "50", "lon": "3", "title": "my title", "content": "my content"}]
を、私はそれとそれをconsole.log
とき、私はデータを参照してください次のように表示されますので、実行するための私のコードを取得するために、私は "を必要とする
[] 0 {key : "value"}
:私は$scope.array.push(obj);
をすればFirebaseからデータを取得するために
[]
0: "{ "lat": "50", "lon": "3", "title": "my title", "content": "my content" }"
、私はこれを取得しますキーも引用符で囲みます。
他の問題は、データがscope.arrLocations
変数に返される前に、私の指示文のlink
関数が実行されていることです。この変数の変更を受け入れることを期待しているwatch
があります。だから私はデータが正しくフォーマットされたら、それはうまくいくと思っていました。
はここに完全に私のコードです:
app.factory('map', ['$q', function($q){
var map={};
var mapInfoItems=[];
map.getMapInfo = function() {
var deferred = $q.defer();
var mapitems = firebase.database().ref('mapinfo/'+firebase.auth().currentUser.uid);
mapitems.on('child_added', function(snapshot){
mapInfoItems.push(snapshot.val());
deferred.resolve(mapInfoItems);
});
//return mapInfoItems;
return deferred.promise;
};
return map;
}]);
app.controller('mapController', ['$scope', 'map', function($scope, map){
$scope.myLocations = {};
$scope.arrLocations = [];
var allLocations = [];
//$scope.arrLocations = [ { "lat": "50", "lon": "3", "title": "my title", "content": "my content" }];
$scope.mapLocations = map.getMapInfo();
map.getMapInfo().then(function(locations){
for(var i=0; i<locations.length; i++){
// create new object each iteration
var obj ={
title : locations[i].name,
content: locations[i].message,
lat : locations[i].lat,
lon : locations[i].lon
}
allLocations.push(obj);
}
$scope.arrLocations = allLocations;
});
}]);
app.directive('myMap', [function() {
// directive link function
var link = function(scope, element, attrs) {
//console.log(scope.myLocations);
console.log('in the link function');
//the line below works and the marker is shown on the map
//scope.arrLocations = [ { "lat": "50", "lon": "3", "title": "my title", "content": "my content" }];
//console.log(scope.arrLocations);
var map, infoWindow;
var markers = [];
//var lastElement = '';
// map config
var mapOptions = {
// center: new google.maps.LatLng(50, 3),
center: new google.maps.LatLng(scope.lastElement.lat, scope.lastElement.lon),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: true
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png'
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function() {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
scope.$watch(function() { return scope.arrLocations; }, function() {
initMap();
console.log(scope.arrLocations);
scope.lastElement = scope.arrLocations[scope.arrLocations.length - 1];
// clear markers
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = [];
angular.forEach(scope.arrLocations, function(value, key){
//console.log('value: ' + value + ' | key: ' + key);
// a single object in this example could be:
// { lat: 50, lon: 3, title: "my title", content: "my content" }
var location = new google.maps.LatLng(value.lat, value.lon);
setMarker(map, location, value.title, value.content);
});
});
};
return {
restrict: 'EA',
//scope: {getMapFn: '&'},
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
}]);
あなたは何をしたいですか? – Sajeetharan
は角度に慣れていませんが、コードを見る方法では、JSONオブジェクトを配列 '$ scope.array.push'にプッシュしているため、配列の最初の位置にあるオブジェクトとして参照されています。 '$ scope.t = array [0];'の前にJSONを_parse_します。 –
私はこのようなオブジェクトの配列を必要とし、残りのコードはこのフォーマットで取得した後にうまく動作するはずです。 '[{" a ":" b "}]' – user1532669