0

I'm trying to update a custom directive in AngularJS - it currently has a fixed template specified within the code (using the template: attribute).

I'd like to allow the user to optionally provide their own template instead, using the templateUrl: attribute.

My problem is how to provide a fallback - I can't use both template and templateUrl in the same directive. This is required to allow backwards compatibility.

I've tried using a function for templateUrl, but returning HTML isn't a runner there.

Any other suggestions?

1 Answer 1

0

If you use function in templateUrl, you are suppose to provide "template identifier" Angular first looks into the template cache, if its not found it tries to do asynchroneous request for the resource of the same url.

So you can simply put your default template into the file and just return its identifier as default.

By the way, if you are using templateUrl and html in separate files, be sure to use some kind of tool like https://www.npmjs.com/package/ng-html2js in the build process.

Sign up to request clarification or add additional context in comments.

Comments

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.