Skip to content Skip to sidebar Skip to footer

Angularjs Ui-router : Conditional Nested Name Views

Following the tutorial : http://scotch.io/tutorials/javascript/angular-routing-using-ui-router and the demo : http://scotch.io/demos/angular-ui-router#/about On the about page , t

Solution 1:

UI Router provides us with two "secret sauces", templateProvider function and the $templateFactory service.

The template provider function which can be injected, has access to locals, and must return template HTML.

So, within this function you can set your conditionals to render a template for a named view. templateProvider() only allows us to return HTML, but let's say we want to return a template for the sake of readability or whatever reason you might have. That's where we'd use $templateFactory and call .fromUrl() on it:

$templateFactory.fromUrl() loads a template from the a URL via $http and $templateCache.

views: {
  'columnOne': {
    controller: 'SomeCtrl',
    templateProvider: function($templateFactory) {
      // if condition is true, return the template for column one
      return $templateFactory.fromUrl('path/to/template.html');
    }
  },
  'columnTwo': {
    controller: 'MaybeSomeOtherCtrl',
    templateProvider: function($templateFactory) {
      // if condition is true, return the template for column two
      return $templateFactory.fromUrl('path/to/template.html');
    }
  }
}

Post a Comment for "Angularjs Ui-router : Conditional Nested Name Views"