2

I've got a directive I built to convert 3-digit ISO-3316 country codes into a flag and the country name. I use it like so:

<country-with-flag data="840">

My directive looks like this:

.directive('countryWithFlag', ['countryCodes', function(countryCodes) {
    return {
        scope: {
            data: '='
        },
        restrict: 'E',
        template: '<img src="assets/images/flags/{{data | numericToAlphaCode}}.png"> {{data | numericToCountry}}',
    }
}])

numericToAlphaCode is a filter which would convert 840 into the 3316-alpha2 code which is "US" in this instance.

The directive results in this: Flag & country name

This works perfectly, except that when the page loads I have an error in the console before angular converts the expression into something useful:

error

Why is this occurring and how can I avoid this error from being generated? I assume I need to do something in the link but I'm a little confused as to how that would work, especially because the error occurs before link even gets called.

1 Answer 1

3

try using ng-src, it should prevent loading of the asset until the string is interpolated

template: '<img ng-src="assets/images/flags/{{data | numericToAlphaCode}}.png"> {{data | numericToCountry}}',
Sign up to request clarification or add additional context in comments.

1 Comment

Well, that fixed it pretty easily! Can't believe I missed that directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.