1

I have static template files (around 50) and I would like to construct ui-states from these template array

var templates = ['index.html','about.html','contact.html','careers.html']

and my state would simply go like this :

$stateProvider
.state('index',{url:'/index.html',templateUrl:'templates/index.html'}) 
.state('about',{url:'/about.html',templateUrl:'templates/about.html'})
.state('contact',{url:'/contact.html',templateUrl:'templates/contact.html'})  

Can I achieve like this?

templates.forEach(function(template){
 $stateProvider.state(template,{url:'/'+template,templateUrl:'templates/'+template}) 
})
1
  • probaly you can do this, but you have to do it with .config. Commented Aug 31, 2016 at 4:46

2 Answers 2

0

simply forEach through your templates and create a state per each template

var templates = [
      'index.html',
      'about.html',
      'contact.html'
    ];

    templates.forEach(function(tmp){
      $stateProvider
        .state(tmp,{
          url:'/'+tmp,
          templateUrl:tmp
        })
    })
Sign up to request clarification or add additional context in comments.

Comments

0

I guess like this,

angular.forEach(templates, function (temp) {
   $stateProvider.state(temp,{
        url:'/' + temp,
        templateUrl:'templates/'+ temp}) 
});

But after this you need to configure this with .config

angular.module('app', [])
     .config({
      // you can add $stateprovider here
     })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.