3

Use case

I have an application that uses both, angular-material (v1.0.0) and ui-router (v0.2.15) and try to have a parent state that has child states which are shown within md-tabs/md-tab.

Problem

I get it working but without the initial tab to be loaded. Only after clicking on the tabs, their state gets loaded.

Code

angular.module('demoApp', ['ui.router', 'ngMaterial'])

  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state('tabs', {
      url: '/tabs',
      template: '<div>' +
                '  <h1>Tabs View</h1>' +
                '  <md-tabs>' +
                '    <md-tab label="Foo" ui-sref="tabs.foo">' +
                '    </md-tab>' +
                '    <md-tab label="Bar" ui-sref="tabs.bar">' +
                '    </md-tab>' +
                '  </md-tabs>' +
                '  <md-content ui-view></md-content>' +
                '</div>'
    });
    $stateProvider.state('tabs.foo', {
      url: '/tabs/foo',
      template: 'Hello from Foo'
    });
    $stateProvider.state('tabs.bar', {
      url: '/tabs/bar',
      template: 'Hello from Bar'
    });
    $urlRouterProvider.when('/tabs', '/tabs/foo');
    $urlRouterProvider.otherwise('/tabs');
  })

;
<!DOCTYPE html>
<html ng-app="demoApp">
<head>
  <link rel="stylesheet" href="https://rawgit.com/angular/bower-material/v1.0.0/angular-material.css">

  <script src="//rawgit.com/angular/bower-angular/master/angular.js"></script>
  <script src="//rawgit.com/angular/bower-angular-aria/master/angular-aria.js"></script>
  <script src="//rawgit.com/angular/bower-angular-animate/master/angular-animate.js"></script>
  <script src="//rawgit.com/angular/bower-material/v1.0.0/angular-material.js"></script>
  <script src="//rawgit.com/angular-ui/ui-router/master/release/angular-ui-router.js"></script>
  <meta charset="utf-8">
  <title>Example</title>
</head>
  
<body>
  
  <div ui-view></div>
  
</body>
</html>

See my example at jsbin.

2
  • 1
    your jsbin doesn't seem to have any code in it.. Commented Dec 15, 2015 at 9:09
  • @Gustav I updated the link, sorry. Commented Dec 15, 2015 at 9:39

3 Answers 3

3
+50

If you open the jsbin output to another window, so that you can watch the url, you will see that redirect to /foo doesn't work.

  1. You have to configure first the urlRouterProvider and afterwards the stateProvider.

  2. Also, from the ui-router wiki:

    When using url routing together with nested states the default behavior is for child states to append their url to the urls of each of its parent states.

So /tabs/foo has to become /foo, the same with /tabs/bar.

( if you want absolute urls you can use url: '^/foo', )

So your code becomes:

angular.module('demoApp', ['ui.router', 'ngMaterial'])

.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.when('/tabs', '/tabs/foo');
    $urlRouterProvider.otherwise('/tabs');

    $stateProvider.state('tabs', {
        url: '/tabs',
        template: '<div>' +
        '  <h1>Tabs View</h1>' +
        '  <md-tabs>' +
        '    <md-tab label="Foo" ui-sref="tabs.foo">' +
        '    </md-tab>' +
        '    <md-tab label="Bar" ui-sref="tabs.bar">' +
        '    </md-tab>' +
        '  </md-tabs>' +
        '  <md-content ui-view></md-content>' +
        '</div>'
    });
    $stateProvider.state('tabs.foo', {
        url: '/foo',
        template: 'Hello from Foo'
    });
    $stateProvider.state('tabs.bar', {
        url: '/bar',
        template: 'Hello from Bar'
    });
});

Working jsbin

Sign up to request clarification or add additional context in comments.

Comments

1

For more control, use a controller function on the parent state and set a default tab in a angular.value()

angular.module('demoApp', ['ui.router', 'ngMaterial'])
  .value('home_tab', 'tabs.foo')
  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider.state('tabs', {
      url: '/tabs',
      template: '<div>' +
                '  <h1>Tabs View</h1>' +
                '  <md-tabs>' +
                '    <md-tab label="Foo" ui-sref="tabs.foo">' +
                '    </md-tab>' +
                '    <md-tab label="Bar" ui-sref="tabs.bar">' +
                '    </md-tab>' +
                '  </md-tabs>' +
                '  <md-content ui-view></md-content>' +
                '</div>',
      controller: ['$state','home_tab', function($state, home_tab){$state.go(home_tab);}]
    });
    $stateProvider.state('tabs.foo', {
      url: '/tabs/foo',
      template: 'Hello from Foo'
    });
$stateProvider.state('tabs.bar', {
  url: '/tabs/bar',
  template: 'Hello from Bar'
});
$urlRouterProvider.otherwise('/tabs');

})

Comments

0

Check below js fiddle may be it solves your issue "https://jsfiddle.net/v8kk5fqq"

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.