回调中的指令集中的双向绑定变量不更新外部变量

时间:2017-04-16 21:57:27

标签: javascript angularjs google-maps angularjs-directive

我已经在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变量在内部变量(和它确实)时更新的想法。

0 个答案:

没有答案