AngularJS ng-href and svg xlink

后端 未结 7 1520
一向
一向 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:

    
    
    

提交回复
热议问题