0

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.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Keshav
  • 821
  • 2
  • 12
  • 33

5 Answers5

3

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:

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
1

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.

Keshav
  • 821
  • 2
  • 12
  • 33
0

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

$stateProvider.state('home', {
  templateUrl: function ($stateParams){
    $http....success({
      return <whatever> + '.html';
    })
  }
})
Andre Kreienbring
  • 2,457
  • 11
  • 16
0

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);
           });
     },   
   }
});
ashr
  • 299
  • 6
  • 13
0

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;
                });
},
Christopher
  • 1,019
  • 1
  • 7
  • 12