HTML object data not updated while scope is

天大地大妈咪最大 提交于 2019-12-11 12:43:12

问题


Using AngularJS and svg-pan-zoom, I display external svg using an <object> as follows :

<object ng-show="currentCartography" id="svgElement" type="image/svg+xml" data="{{currentCartography}}.svg">
It seems your browser doesn't support SVG.
</object>

The issue is that when I change my svg using a function like this :

<a href="#" onclick="changeSVG('tiger')">Tiger</a><br />
<a href="#" onclick="changeSVG('Tux')">Tux</a><br />

Only the scope variable is updated. I have to click twice on the link to also update the data of the html <object>.

Here is the changeSVG(fileName) function :

Out of my controller :

// Out of the controller
function    changeSVG(fileName) {
   var scope = angular.element(document.getElementById("svgElement")).scope();

   scope.$apply(function() {
       scope.changeSVG(fileName);
   })
}

And in my controller :

// Inside controller
$scope.changeSVG = function (fileName) {
    console.log("HTML Object data before : " + document.getElementById("svgElement").getAttribute("data"));
    console.log("Scope currentCartography before : " + $scope.currentCartography + "\n--");
    $scope.currentCartography = fileName;
    window.panZoom.destroy();
    svgPanZoom('#svgElement').destroy();
    window.panZoom = svgPanZoom('#svgElement', {
        zoomEnabled: true,
        controlIconsEnabled: true,
        fit: true,
        center: true,
        minZoom: 0.75,
        maxZoom: 50,
        zoomScaleSensitivity: 0.25,
        dblClickZoomEnabled: true
    });
    console.log("HTML Object data after : " + document.getElementById("svgElement").getAttribute("data"));
    console.log("Scope currentCartography after : " + $scope.currentCartography + "\n-----");
};

I'm using the console to check some values and indeed, the scope variable are updated when I call my function, but the data attribute of my html <object> is not until I click a second time. Any idea of what's going on ? I want my html to be updated in one click, not two.

Here is a live example, click on the menu on the left to change SVG and see the behavior : http://embed.plnkr.co/0nufcYZpE3ZzGxKQmUkf/preview (This might not work on IE)

来源:https://stackoverflow.com/questions/30051962/html-object-data-not-updated-while-scope-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!