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.

3 Answers 3

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

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

Comments

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'
});

1 Comment

I don't think you can have an empty url. You can, however, have a "/" url, and add a $urlRouterProvider.when('/employees', '/employees/');
1

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

$stateProvider.state('contacts', {
    url: "/contacts",
    ...
}

4 Comments

it works now, thanks :) sometimes we need more coffee :)
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.
$stateProvider.state('employees.list', { url: "/employees/list", // Note the empty URL templateUrl: '/static/partials/employee/employee-list' });
same issue if i remove the nested states.

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.