AngularJS ng-href and svg xlink

后端 未结 7 1503
一向
一向 2020-12-13 17:53

I\'d like some input on using xml namespaced attributes with angular.

The problem is angular comes with a couple of directives to handle writing attributes such as h

相关标签:
7条回答
  • 2020-12-13 18:37

    I solved the same problem with the following modules:

    Module for SVGs:

    var app = angular.module('Svgs', []);
    
    angular.forEach([
        { ngAttrName: 'ngXlinkHref', attrName: 'xlink:href' },
        { ngAttrName: 'ngWidth', attrName: 'width' },
        { ngAttrName: 'ngHeight', attrName: 'height' }
    ], function (pair) {
    
        var ngAttrName = pair.ngAttrName;
        var attrName = pair.attrName;
    
        app.directive(ngAttrName, function (IeHelperSrv) {
    
            return {
    
                priority: 99,
    
                link: function (scope, element, attrs) {
    
                    attrs.$observe(ngAttrName, function (value) {
    
                        if (!value) return;
    
                        attrs.$set(attrName, value);
                        if (IeHelperSrv.isIE) element.prop(attrName, value);
                    });
                }
            };
        });
    });
    

    Module for IE detection:

    angular.module('IeHelper', []).factory('IeHelperSrv', function () {
    
        return {
            isIE: checkForIE.isIE,
        }
    });
    
    var checkForIE = {
        init: function () {
            this.isIE = (navigator.userAgent.indexOf('MSIE') != -1);
        }
    };
    
    checkForIE.init();
    

    HTML:

    <!-- image has initial fake source, width and height to force it to render -->
    <image xlink:href="~/Content/Empty.png" width="1" height="1"
        ng-xlink-href="{{item.imageSrc}}"
        ng-width="{{item.width}}" ng-height="{{item.height}}"
        ng-cloak
        />
    
    0 讨论(0)
提交回复
热议问题