0

I have the simplest ui.router example which doesn't seem to work.

See below:

var routerApp = angular.module('mainApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider) {   

    //$urlRouterProvider.otherwise('/home'); When I uncomment this, it loads /home without a problem but why is it going to the "otherwise" condition?

    $stateProvider       
    .state('home', {
        url: '/home',
        templateUrl: 'main.html'
    })
});

My index file.

<!DOCTYPE html>
<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
   <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
    <script src="/angular/angularApp.js"></script>
</head>

<body ng-app="mainApp">

<div class="container">
    <div ui-view></div>
</div>

</body>
</html>

I have been going on and on into this but I think I now have tunnel vision. Someone please help me out here. Thanks in advance!

2
  • Can you post the html and js/ts file where you defined the angular component ui-view (not sure if that's the one)
    – Ziku
    Jul 28, 2021 at 3:05
  • This is angularjs not angular Jul 28, 2021 at 3:05

1 Answer 1

0

I do not see any issue except you having the same controller for both, can you check the example below

app.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise("/");
  $stateProvider.state('top', {
      url: "",
      templateUrl: "header.html",
      controller: 'topCtrl'
    })
    .state('top.side', {
      url: '/app',
      templateUrl: "side.html",
      controller: 'filterCtrl'
    })
    .state('top.side.list', {
      url: "/items?query",
      templateUrl: "items.html",
      controller: 'listCtrl'
    })
    .state('top.side.item', {
      url: "/:id",
      templateUrl: "item.html",
      controller: 'itemCtrl'
    });
});

DEMO

1
  • This works like a charm! I updated the versions thought as it was using a very old one but it worked without any further changes. Thanks a lot mate!
    – Sean
    Jul 28, 2021 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.