Chrome中的SVG"使用"标记已损坏

Vit*_*kov 10 svg google-chrome angularjs

SAP(AngularJS和Angular Route)具有由svg-sprite制作的基于图标的导航.所以,我有这样的内联代码:

<div style="height: 0; width: 0; position: absolute; visibility: hidden">
<svg xmlns="http://www.w3.org/2000/svg">
    <symbol id="icon-grid-32" viewBox="0 0 32 32">
        <g stroke-width="2" stroke-linecap="round" stroke-miterlimit="10" stroke-linejoin="round">
            <path d="M2 2h11v11H2zM19 2h11v11H19zM2 19h11v11H2zM19 19h11v11H19z"/>
        </g>
    </symbol>
</svg>
</div>
Run Code Online (Sandbox Code Playgroud)

导航中的图标如下:

<a href=""><svg class="icon icon-32 outline black"><use xlink:href="#icon-grid-32"></use></svg></a>
Run Code Online (Sandbox Code Playgroud)

我在这个导航中看到的所有东西都没有,<use>大小为0×0像素.我知道有关xml:base的 Firefox bug,但添加xml:base对我没有帮助.你可以尝试这个例子:http://css.yoksel.ru/assets/demo/svg-in-firefox/svg-has-base.html

它适用于Firefox,Safari和其他浏览器,但不适用于Chrome 49+(48版本没有此问题).

Tia*_*Lin 5

这是由AngularJS的依赖性<base href="/" />和UI路由组合引起的,当应用程序未处于"根"状态时,<use>元素中的相对散列链接将无法正确解析.

要解决这个问题,您需要解决xlink:href使用绝对路径的问题.您可以执行以下操作:

角图标-template.html

<svg class="c-icon" viewBox="0 0 32 32">
    <use xlink:href="" ng-attr-xlink:href="{{baseUrl + '#' + iconName}}" />
</svg>
Run Code Online (Sandbox Code Playgroud)

角icon.js

angular.module('angularIcon', [])
    .directive('angularIcon', {
        templateUrl: 'angular-icon-template.html',
        scope: { iconName: '@' },
        controller: function($scope) {
            $scope.baseUrl = window.location.href.replace(window.location.hash, '');
        }
    });
Run Code Online (Sandbox Code Playgroud)


小智 1

我遇到了与您所描述的非常相似的问题,不同之处在于我会在指令中生成<svg>图标<use>

今天的大部分时间我一直在寻找答案,并提出了解决该<use>问题的方法xlink:href。它只是通过内联所需的 svg 来重新创建功能。

为了简单起见,假设我有一个<angular-icon>通过属性接收图标名称的指令icon-name

<angular-icon icon-name="{{someObject.iconName}}">

工作指令现在如下所示:

angular.module('angularIcon', [])
.directive('angularIcon', IconDirective);

function IconDirective(){
    return{
        template:'',
        templateNamespace:'svg',

        link:function(scope, element, attributes){

            // my icon name comes from $http call so it doesnt exist when initialising the directive, 
            attributes.$observe( 'iconName', function(iconName){

                // let's grab the icon from the sprite
                var icon = angular.element( document.getElementById( iconName ) ); 
                // let's clone it so we can modify it if we want
                var iconClone = icon.clone();

                var namespace = "http://www.w3.org/2000/svg";

                // manually create the svg element and append the inlined icon as no other way worked
                var svg = document.createElementNS( namespace, 'svg' );
                svg.setAttribute( 'viewBox', '0 0 32 32' );
                svg.setAttribute( 'xml:space', 'preserve' );

                svg.appendChild( iconClone[0] );
                // let's add the newly created svg+icon to the directive's element
                element[0].appendChild( svg );

            });

        },
        scope:{
            iconName: '@'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)