我尝试使用AngularJS实现Google Maps infoWindow。
我有一个模板
<div>{{foo}}<br/>
,并且有 $scope.foo="bar"
。
我希望在信息窗口中看到值"bar",而不是看到"{{foo}}"。因此,我编译并设置了内容,但还没有成功。
一些人可能认为这与AngularJS ng-include inside of Google Maps InfoWindow?相同,但我想动态编译内容。
这是我现在拥有的代码,http://plnkr.co/edit/RLdAcaGz5FfmJEZi87ah?p=preview
angular.module('myApp', []).controller('MyCtrl', function($scope, $compile) {
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var mapOptions = { zoom: 4, center: myLatlng };
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var infowindow = new google.maps.InfoWindow({position: myLatlng});
var x = "<div>{{foo}}<br/>{{foo}}";
$scope.foo="bar";
var el = $compile(x)($scope);
infowindow.setContent(el.html());
infowindow.open(map);
})
我在思考 $scope.apply()
,但是在代码中无法运行。
是否有一种方法可以对谷歌地图InfoWindow进行$compile
编译?