【问题标题】:AngularJS ng-repeat + SVG binding Bug in IE10?IE10 中的 AngularJS ng-repeat + SVG 绑定错误?
【发布时间】:2013-06-08 04:36:47
【问题描述】:

我正在尝试使用 AngularJS 来填充 SVG。在 Firefox 和 Chrome 中,这可以完美运行,但是,在 IE 中,我只能看到最后一个形状(无论是哪种形状)。

HTML:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="300"
    overflow="hidden" ng-controller="MyCtrl" ng-cloak>

    <rect fill="white" x="0" y="0" width="400" height="300" />

    <rect ng-repeat="item in items"
        width="{{item.width}}"
        height="{{item.height}}"
        z="{{item.z}}"
        fill="{{item.fill}}"
        transform="translate({{item.tx2}} {{item.ty2}})
            rotate({{item.angle}})
            scale({{item.scaleX}} {{item.scaleY}})
            translate({{item.tx1}} {{item.ty1}})" />

</svg>

JavaScript:

var app = angular.module('MyModule', [])
    .controller('MyCtrl', function ($scope, $http, AudioSrv) {

    var demoItem = function (cnt, color) {
        item = {};
        item.x = 0;
        item.y = 0;
        item.width = 100;
        item.height = 100;
        item.fill = color;
        item.z = cnt + 1;
        return item;
    };

    var setItem = function (item, angle, scaleX, scaleY, x, y) {
        item.tx1 = -item.width * 0.5;
        item.ty1 = -item.height * 0.5;
        item.angle = angle;
        item.scaleX = scaleX;
        item.scaleY = scaleY;
        item.tx2 = x;
        item.ty2 = y;
        return item;
    };

    $scope.items = [
        setItem(demoItem(0, 'black'), 0, 2, 2, 0, 0),
        setItem(demoItem(1, 'blue'), 0, 1, 1, 0, 0),
        setItem(demoItem(2, 'orange'), 0, 0.5, 0.5, 0, 0),
        setItem(demoItem(3, 'green'), -90, 2, 2, 200, 150),
        setItem(demoItem(4, 'purple'), 45, 0.5, 0.5, 200, 150)
    ];
});

这是 IE 中的已知错误吗?有什么解决方法吗?

编辑:

经过进一步调查,似乎 ng-repeat 中的所有项目都被替换为最后一个项目,这也发生在 SVG 之外,输入绑定到同一个集合。

【问题讨论】:

    标签: html internet-explorer angularjs svg cross-browser


    【解决方案1】:

    您正在重复使用(因此,每次都覆盖以前的版本)item 变量,因为您没有创建它的新范围版本:

    var demoItem = function (cnt, color) {
      item = {}; <-- here
      ...
    

    改用这个:

    var demoItem = function (cnt, color) {
      var item = {};
      ...
    

    【讨论】:

    • 谢谢。它以某种方式在 Firefox 和 Chrome 上运行的事实完全让我偏离了道路。
    • @DannyVarod 是的,它可以在 FF/Chrome 上运行相当令人惊讶 :)
    猜你喜欢
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2017-12-22
    • 2015-05-13
    相关资源
    最近更新 更多