2

I don't manage to route using ui-router. It does work for me with ng-route, so I must have missed something basic. There is no error but I don't get any view.

The following code does not work (except for the otherwise statement):

angular.module("employeesApp", ["ui.router"])
.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('employeeList', {
            url: "/employees",
            templateUrl: "components/employee_list/employeeListView.html",   
        });

    $urlRouterProvider.otherwise("/employees");
});

The following code does work:

angular.module("employeesApp", ["ngRoute"])
.config(function ($routeProvider) {
    var employeeListRoute = {
        templateUrl: "components/employee_list/employeeListView.html"
    };

    var otherwiseRoute = employeeListRoute;

    $routeProvider
        .when("/employeeList", employeeListRoute)
        .otherwise(otherwiseRoute);

Here is my index.html (omitted not relevant elements):

<!DOCTYPE html>
<html lang="en" ng-app="employeesApp">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.js"></script>

    <script src="app.route.js"></script>
    <script src="components/employee_list/employeeListCtrl.js"></script>
</head>
<body>
    <div>
        <ng-view />
    </div>
</body>
</html>

What am I doing wrong when trying to use ui-router?

6
  • Any error in console? have you included angular-ui-router.js? Commented Jul 14, 2016 at 15:18
  • @Pankaj Parkar there are no errors in the console, and I have included angular-ui-router.js. As a proof: the otherwise statement does work. Commented Jul 14, 2016 at 15:21
  • 2
    There is a good chance you are missing the ui-view directive: <body><div ui-view></div></body>. Do you have ui-view anywhere to load the views ? Commented Jul 14, 2016 at 15:24
  • @Amin Meyghani I don't miss it, I have it. I have mentioned that the code that uses ng-route does work. Commented Jul 14, 2016 at 15:26
  • can you paste in the code where you load the view ? Commented Jul 14, 2016 at 15:26

1 Answer 1

3

As @AminMeyghani already said, you should be using ui-view instead of ng-view directive to get router know that relative view should be loaded inside ui-view directive

<div>
    <ui-view></ui-view>
</div>

Demo here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.