Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular Directive Fix #24

Open
martyzz1 opened this issue Jul 12, 2017 · 0 comments
Open

Angular Directive Fix #24

martyzz1 opened this issue Jul 12, 2017 · 0 comments

Comments

@martyzz1
Copy link

We had some issues with svg's who's data-src values were dynamic
e.g.
<svg data-src="{{$ctrl.path_to_svg"">
The main issue is that the directive was updating prior to the digest cycle completing, and because there was no $watch setup to listen for subsequent changes....

Feel free to copy the function link function below into the main code, and for everybody else who wants to run the fix in the interim you can use the following decorator...

angular.module("app").config(['$provide', function($provide) {

        $provide.decorator("svgDirective", ['$delegate', 'svgInjectorFactory', '$timeout',
            function($delegate, svgInjectorFactory, $timeout) {
                var directive = $delegate[0];
                var cfg = svgInjectorFactory.getConfig();
                var link = function(scope, element, attrs) {
                    if (attrs["class"] && attrs["class"].indexOf(cfg.spriteClassIdName) >= 0) {
                        attrs.$observe("class", function() {
                            svgInjectorFactory.inject(element[0]);
                        });
                    } else if (attrs.dataSrc) {
                        $timeout(function() {
                            svgInjectorFactory.inject(element[0]);
                        });
                    } else if (attrs.src) {
                        $timeout(function() {
                            svgInjectorFactory.inject(element[0]);
                        });
                    }

                    scope.$watch('dataSrc', function(newVal, oldVal) {
                        if (oldVal !== newVal) {
                            console.log('dataSrc watch changed', element[0], newVal, oldVal);
                            svgInjectorFactory.inject(element[0]);
                        }
                    });
                };

                directive.compile = function() {
                    return function(scope, element, attrs) {
                        link.apply(this, arguments);
                    };
                };

                return $delegate;
            }]);
        }]);


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant