Keshav
Keshav

Reputation: 831

load template url from $http request in state routing

I want to load templateurl from $http call.

$stateProvider
.state('home', {
        url: '/',
        templateUrl: will be served by $http call from server
    });

Please suggest how i will implement this.

Upvotes: 0

Views: 2018

Answers (5)

Christopher
Christopher

Reputation: 1029

I used templateProvider (https://github.com/angular-ui/ui-router/wiki) and retrieved a JSON object that contained my template doing something like this:

templateProvider: function($http) {
        return $http.get("http://example.com/returnJSONobject")
                .then(function(res) {
                    return res.htmlTemplate;
                });
},

Upvotes: 0

Keshav
Keshav

Reputation: 831

Angular has done it it in most elegant way .If you want to call your backend controller to request a jsp page for templateurl just call $http url like this

.state('inbox', {
        url: '/inbox',
        templateUrl:'jspController/inboxRecord',
        controller : 'inboxController'
    }) 

that url respond with jsp page in my case inboxrecord.jsp that will be rendered.

Upvotes: 1

ashr
ashr

Reputation: 299

You will need a controller to populate your template from the results, and bind scope values from your template.

 $stateProvider
.state('home', {
    url: '/',
    templateUrl: 'index.html',
    resolve {
        results:  function($http){
        return $http({method: 'GET', url: '/serverspi'})
           .then (function (data) {
               return process(data);
           });
     },   
   }
});

Upvotes: 0

Radim Köhler
Radim Köhler

Reputation: 123861

I would suggest to use the templateProvider. That could consume any amount of IoC params (including current $stateParams). Combined with $templateRequest, we also can easily get template from server and keep it cached:

.state('myState', 
{
    ...
    // instead of templateUrl
    templateProvider: ['$templateRequest', '$stateParams', 
    function($templateRequest,$stateParams){
      var pathToTemplate = '....html';
      // get the proper path from any IoC injected here

      return $templateRequest(pathToTemplate);

    }],

Check these:

Upvotes: 3

André Kreienbring
André Kreienbring

Reputation: 2509

as templateUrl can also be a function you could easily do:

$stateProvider.state('home', {
  templateUrl: function ($stateParams){
    $http....success({
      return <whatever> + '.html';
    })
  }
})

Upvotes: 0

Related Questions