I have just added a CDN service to make my website load faster.
I have a question regarding photos that are fetched using a <img> tag.
I have many cases like this:
// userProfile.thumbnailPhotoSrc = some/relative/path.png
ng-src="{{userProfile.thumbnailPhotoSrc}}"
(I use AngularJS).
On startup, I have a script that saves the CDN endpoint to window.cdn variable. In case the script decides there's no endpoint available:
window.cdn = '';
So I want to be able to do something like:
ng-src="window.cdn + {{userProfile.thumbnailPhotoSrc}"
But this is impossible, as window.cdn is not evaluated. But neither this is working:
ng-src="{{window.cdn + userProfile.thumbnailPhotoSrc}"
Because I can only access $scope properties under {{}}.
I can't save the CDN endpoint to $scope, because there exists a $scope for every controller (I have many, this is not maintainable).
The last thing I thought of, hoping ng-src allows that - is adding a transformation function that is run on any ng-src attribute, but I could not find how to do that.
What do you suggest I do? keep in mind that I would like the website fetch the photo from the CDN and fallback to the origin server (fetch the relative path) in case of CDN malfunction. How can I obtain that behavior?
Thanks
You could create a custom filter in Angular
You would then have then in your templates:
You could add the
$windowservice as a dependency to the filter service so you don't have to pull it from the global object and you can test the filter as well.