0

I want add to add some headers to the requests for fetching templates so I can reject non-angularjs requests and respond with an error.

However it seems that it's only possible using $http and it seems like terrible waste to reimplement a mechanism similar to the one templateUrl provides.

Here's my sample router:

siteApp.config([ '$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider.when('/user', {
                controller : 'UserController',
            }).when('/link', {
                templateUrl : '/user/index.json'
            });
            $locationProvider.html5Mode(true);
        } ]);

edit: if it helps, I'm trying to get my ZendFramework2 API to detect it, but it fails to recognize the XmlHttpRequest

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
mewa
  • 1,532
  • 1
  • 12
  • 20

1 Answers1

0

I'd suggest you to load those template at the run phase of angular. That will be putted inside $templateCache provider and when you request the same template for second time it will fetch directly from $templateCache itself.

CODE

//get the templates with headers in application run phase.
app.run(['$templateCache', '$q', '$http', function($templateCache, $q, $http) {
    var promises = [];
    $http.get('partials/views/template1.html', {
        headers: {
            'Content-Type': undefined //here you can add multiple header and get the template
        },
        cache: $templateCache
    });
    $http.get('partials/views/template2.html', {
        headers: {
            'Content-Type': undefined //here you can add multiple header and get the template
        },
        cache: $templateCache
    });
}]);

Hope this could help you. Thanks.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299