Force HTTP interceptor in dynamic ngSrc request

后端 未结 4 1092
一整个雨季
一整个雨季 2020-12-03 03:53

I\'ve created an AngularJS application that loads images from an OAuth secured backend. My OAuth library is configured by adding an extra interceptor to the Angular $h

4条回答
  •  温柔的废话
    2020-12-03 04:08

    This is a little old, but thought I would add another method for finding an image w/o using an interceptor. In my case, I needed to find the image path from the rails assets pipeline.

    There was a blog from Codetunes (http://codetunes.com/2014/5-tips-on-how-to-use-angularjs-with-rails-that-changed-how-we-work/) that described how they find templates. This makes use of an interceptor but doesn't work for images.

    The solution I used requires you to use a model for the ng-src value. Within the model, I call a helper method from a service that utilizes the Rails.templates variable setup from the above blog article.

    So, it looks like the following:

    angular.module('myApp').factory('RailsAssetService', function(Rails) {
    
        function RailsAssetService() {
    
        }
    
        RailsAssetService.getAssetsUrl = function(url) {
            var railsUrl = Rails.templates[url];
            if (railsUrl === undefined)
                railsUrl = url;
            return railsUrl;
        }
    
        return RailsAssetService;
    });
    

    and in the controller:

    vm.creditCardsImage = RailsAssetService.getAssetsUrl('credit-cards-logo.gif');
    

    and in the view:

    
    

提交回复
热议问题