0

I am trying to use ui-router on my project.

Core module:

  var core = angular.module('muhamo.core', ['angular-loading-bar', 'anguFixedHeaderTable', 'ui.router']);

Tracking module:

    var app = angular.module(TRACKING_MODULE_NAME, ['muhamo.core']);
    app.config(Configure);

Configure.$inject = ['$stateProvider', '$urlRouterProvider'];
function Configure($stateProvider, $urlRouterProvider) {
    $stateProvider.state('contacts', {
        templateUrl: '/static/partials/employee/employee-edit',
        controller: function () {
            this.title = 'My Contacts';
        },
        controllerAs: 'contact'
    });
    $urlRouterProvider.otherwise("/contacts");
    console.log($stateProvider);
}

and the html definition :

    <div  ui-view></div>

It works fine if i click to a ui-sref link. But on page load it does not load the default view "/contacts". Am I missing something here?

UPDATE

It works after adding missing "url" property. But now I've another problem, if I extend my implementation like that :

    function Configure($stateProvider, $urlRouterProvider) {
       $stateProvider.state('employees', {
          abstract: true,
          url: "/employees"
          /* Various other settings common to both child states */
        }).state('employees.list', {
           url: "", // Note the empty URL
           templateUrl: '/static/partials/employee/employee-list'
       });
    $urlRouterProvider.otherwise("/employees");
    console.log($stateProvider);
}

also with more states, ui-view is not rendering.

aymeba
  • 934
  • 1
  • 11
  • 26

3 Answers3

1

Yes. You need to set the state.url to '/contacts'

$stateProvider.state('contacts', {
    url: '/contacts',
    templateUrl: '/static/partials/employee/employee-edit',
    controller: function () {
        this.title = 'My Contacts';
    },
    controllerAs: 'contact'
});
Dan Crews
  • 3,067
  • 17
  • 20
  • I don't think you can have an empty url. You can, however, have a "/" url, and add a `$urlRouterProvider.when('/employees', '/employees/');` – Dan Crews Oct 13 '15 at 17:28
1

It seems you forgot to set the url parameter, e.g.:

$stateProvider.state('contacts', {
    url: "/contacts",
    ...
}
Dzinx
  • 55,586
  • 10
  • 60
  • 78
  • it works now, thanks :) sometimes we need more coffee :) – aymeba Oct 13 '15 at 17:10
  • Basically, you need a dispatcher in the parent state if you want to use nested states with empty URLs. See [the second part of my answer to another question](http://stackoverflow.com/a/32712889/18745). – Dzinx Oct 13 '15 at 17:21
  • $stateProvider.state('employees.list', { url: "/employees/list", // Note the empty URL templateUrl: '/static/partials/employee/employee-list' }); – aymeba Oct 13 '15 at 17:24
  • same issue if i remove the nested states. – aymeba Oct 13 '15 at 17:24
1

There are two fishy things in your implementation. You out an empty url and your default route is abstract. Try my changes below.

 function Configure($stateProvider, $urlRouterProvider) {
   $stateProvider.state('employees', {
      abstract: true,
      url: "/employees"
      /* Various other settings common to both child states */
    }).state('employees.list', {
       url: "/list", // Note the empty URL
       templateUrl: '/static/partials/employee/employee-list'
   });
$urlRouterProvider.otherwise("/employees/list");
console.log($stateProvider);

Cheers

atardadi
  • 429
  • 2
  • 5
  • 14