0

Is it possible to use templateUrl instead of the embedded template in ui-router? If yes, which approach is better?

EddyG
  • 2,051
  • 3
  • 22
  • 46

1 Answers1

1

Yes, you can use templateUrl in ui-router.

.state('stateName', {
    templateUrl: 'path/to/template',
    controller: 'someController'
});

But which is the best approach is a trick question. Its all about how you are going to use the state. For example,

  1. if your using angular 1.5 components, and need to call it as page, then it better to use template;

.state('stateName', { template: '<component-name></component-name>' });

This is one of the most common approch for component. If you using ui-router version above 1.5, then you can do following too,

.state('stateName', {
     component: 'component-name'
 })
  1. If you are using seperate template and controller it is better to use templateUrl,

.state('stateName', { templateUrl: 'path/to/template', controller: 'someController' })

But directly writing the html like below is considered as bad practice

.state('stateName', {
    templateUrl: '<div><p>never do this</p></div>', 
    controller: 'someController'
});
Amir Suhail
  • 1,284
  • 3
  • 12
  • 31
  • You think which approach is better for reusability? I want to apply it for developing component based software product line. Can I create an Angular Component that uses templateUrl instead of template? – EddyG Mar 14 '17 at 10:13
  • 1
    If you want to create reusable components, then defenitily you should stick with angular component, in that I would use template as mentioned in example 1 in the answer – Amir Suhail Mar 14 '17 at 10:18
  • And you suggest using ui-router better than ng-route? – EddyG Mar 14 '17 at 10:22
  • 1
    Thats explained better here http://stackoverflow.com/questions/21023763/what-is-the-difference-between-angular-route-and-angular-ui-router – Amir Suhail Mar 14 '17 at 10:28