jVectorMap 使用新的参考地图更改/刷新地图?

use*_*179 1 html angularjs jvectormap

我有一个包含地图的 div,但是,我想更改一个属性,该属性又会更新地图以对应于对该属性所做的更改。更具体地说,当我更改地图名称时,我希望 div 发生更改以反映这个新的地图名称。如果我绘制了智利地图并将地图名称更改为巴西,我希望绘制巴西的地图。请参阅下文了解更多背景信息。我已经尝试过.reset().updateSize()以及建议的其他一些调整...没有任何效果。

app.directive('countrymap', ['$rootScope', function($rootScope) 
{
    return {
        link: function(scope, element, attrs) {
            $(element).width('auto'),
            $(element).height(500),
            $rootScope.$watch("countryMap", function (newCountry, oldCountry) 
            {
                setTimeout( function() 
                { 
                    var map = new jvm.Map(
                    {
                        container: $(element),
                        map: newCountry,
                        backgroundColor: 'transparent',
                        regionsSelectable: true,
                        regionsSelectableOne: true,
                        zoomButtons : false,
                        regionStyle: 
                        {
                            selected:
                            { fill: 'Red' }
                        }
                    });
                }, 100);
            })
        }
    };  
}]);
Run Code Online (Sandbox Code Playgroud)

use*_*179 5

我想我应该为那些遇到同样挫折的人发布我找到的解决方案。我决定扔掉.remove()旧地图并绘制一张新地图。请参阅下面的修改后的代码。

app.directive('countrymap', ['$rootScope', function($rootScope) 
{
    return {
        link: function(scope, element, attrs) {
            $(element).width('auto'),
            $(element).height(500),
            $rootScope.$watch("countryMap", function (newCountry, oldCountry) 
            {
                setTimeout( function() 
                { 
                    try { $(element).vectorMap('get', 'mapObject').remove(); }
                    catch(err) {}
                    var map = new jvm.Map(
                    {
                        container: $(element),
                        map: newCountry,
                        backgroundColor: 'transparent',
                        regionsSelectable: true,
                        regionsSelectableOne: true,
                        zoomButtons : false,
                        regionStyle: 
                        {
                            selected:
                            { fill: 'Red' }
                        }
                    });
                }, 100);
            })
        }
    };  
}]);
Run Code Online (Sandbox Code Playgroud)