1

I have this Angular code:

    .state('UserTables', {
        url: '/Tables',
        resolve: {
            auth: function resolveAuthentication(SessionService) {
                return SessionService.isUser();
            }
        },
        views: {
            "containerMain": {
                templateUrl: 'Views/Tables',
                controller: TableController
            },
        }
    })

And would like to pass some request header to the templateUrl call.

Anyone done something like that?

Basically I have a REST service that can generate the view I need depending on 1 header and some property's. Property's are no problem but I have no clue how to make a call to the service and wait for the result.

Tried:

        views: {
            "containerMain": {
                template: function (SessionService, $http, $q) {
                    console.log('template');
                    var resp = SessionService.getTable($http, $q, 'Generate/Table?objectId=WfObject');
                    var r = '';
                    resp.then(function (result) {
                        r = result;
                        console.log('resp:', r);
                    });
                    console.log('r:',r);
                    return r;
                }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jester
  • 3,069
  • 5
  • 30
  • 44

2 Answers2

2

I created working plunker here

To load template with custom headers, we can call do it like this (check the state 'UserTables' in the plunker):

views: {
    "containerMain": {
        //templateUrl: 'Views/Tables',
        templateProvider: ['$http',
          function ($http) {

            var tplRequest = {
              method: 'GET',
              url: 'Generate/Table?objectId=WfObject',
              headers: {
                'MyHeaderKey': 'MyHeaderValue'
              }, 
            }

            return $http(tplRequest)
            .then(function(response) {
                console.log('loaded with custom headers')
                var tpl = response.data;
                return tpl;
              }
            );
        }],
        controller: 'TableController'
    },
  }

In case, we want (and can) cache the template, we can do it like this (check the state 'UserTablesWithCache'):

views: {
    "containerMain": {
        //templateUrl: 'Views/Tables',
        templateProvider: ['$http', '$templateCache',
          function ($http, $templateCache) {

            var templateName = 'Generate/Table?objectId=WfObject';
            var tpl = $templateCache.get(templateName)
            if(tpl){
              console.log('returning from cache');
              return tpl;
            }

            var tplRequest = {
              method: 'GET',
              url: templateName,
              headers: {
                'MyHeaderKey': 'MyHeaderValue'
              }, 
            }

            return $http(tplRequest)
            .then(function(response) {
                console.log('loaded, placing into  cache');
                var tpl = response.data;
                $templateCache.put(templateName, tpl) 
                return tpl;
              }
            );
        }],
        controller: 'TableController'
    },
  }

And if we would not need headers, and we could cache, it is really very easy, as documented here:

Drafted version could be: (no custom headers but effective loading and caching)

templateProvider: ['$templateRequest', function(CONFIG, $templateRequest) {

    var templateName = 'Generate/Table?objectId=WfObject';

    return $templateRequest(templateName);
}],
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

templateUrl property can also take function as value. So you can add dynamic properties to the templateUrl via there.

templateUrl : function(stateParams) {
      // before returning the URL, add additional properties and send
      // stateParamsargument object refers to $stateParams and you can access any url params from there.
      return  'Views/Tables';
}
mohamedrias
  • 18,326
  • 2
  • 38
  • 47