3

When using ui-router, I setup states like so:

$stateProvider
  .state 'product',
    url: '/catalog/product',
    templateUrl: 'app/product/product.html',

The only problem is, I have my controller in a directive, not as a stand-alone controller service. So how do I tell ui-router to hit my directive instead of the template?

Otherwise I'm not sure how to bind the template scope to the controller defined in my directive.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Nathan
  • 7,627
  • 11
  • 46
  • 80
  • If you have a directive inside your template then that directive's controller will automatically come into action. No need to tell ui-router. – Frozen Crayon Mar 02 '15 at 12:59

1 Answers1

6

If you insist on Directive, as a target for your state template, the way would be :

$stateProvider
  .state 'product',
    url: '/catalog/product',
    template: '<div here-is-my-directive-with-all-its-standard-settings></div>',

NOTE: there does not have to be controller explicitly defined for view at all. Just the HTML template will be injected into position in parent

And the directive:

.directive('hereIsMyDirectiveWithAllItsStandardSettings', ...)

Other words, the UI-Router is handling states, and injecting the template. The template contains directive, which can do what was designed for... as expected...

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 4
    How would you pass parent scope variables to your template directive, assuming it has isolate scope? – Will Aug 04 '16 at 04:09