我已经在SO上准备了类似的帖子,但似乎没有人完全削减它。我正在创建一个谷歌地图指令,这反过来依赖于我根据一些读数记下的服务。基本上我的代码是:
var GoogleMapsDirective = function(GoogleMaps) {
return {
restrict: "A",
scope: {
map: "="
},
link: function(scope, element) {
var apiKey = "something";
scope.$watch("map", function(map) {
//Does indeed log twice: "undefined" and, when the callback is
//called, the map object returned by the "new google.maps.Map(...)"
//invokation
console.log(map);
});
GoogleMaps.init(apiKey, element[0]).then(function(map) {
scope.map = map;
//Not good: complains that "$digest already in progress"
//scope.$apply();
});
scope.$on("$destroy", function() {
GoogleMaps.dispose();
});
}
};
};
var myController = function($scope) {
$scope.$watch("map", function(map) {
//this, however, only logs "undefined" the first time
console.info(map);
});
};
其中myController
是视图的控制器,基本上包含<div map="map"></div>
。
我所有关于如何让我的外部$scope.map
变量在内部变量(和它确实)时更新的想法。