AngularJS: Getting ngRoute working
Asked Answered
I

1

8

I'm working on a new Angularjs webapp where I have to use ngRoute. I'm a bit confused first, because routing doesn't work at all in my case – I always end up at index and MainCtrl.

in index.html I've included all dependencies like so:

<body ng-app="orderyourselfApp">
    <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div class="container" ng-controller="LoginCtrl">{{hello}}</div>Soem random stuff

    <!--[if lt IE 9]>
    <script src="bower_components/es5-shim/es5-shim.js"></script>
    <script src="bower_components/json3/lib/json3.min.js"></script>
    <![endif]-->

    <!-- build:js(app) scripts/vendor.js -->
    <!-- bower:js -->
    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-resource/angular-resource.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

        <!-- build:js({.tmp,app}) scripts/scripts.js -->
        <script src="scripts/app.js"></script>
        <script src="scripts/controllers/main.js"></script>
        <script src="scripts/controllers/navbar.js"></script>
        <!-- endbuild -->
</body>

And I have the routes set up in app.js

'use strict';

angular.module('orderyourselfApp', [
  'ngResource',
  'ngRoute'
])
  .config(function ($routeProvider, $locationProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'partials/main',
        controller: 'MainCtrl'
      })
      .when('/login', {
        templateUrl: 'login',
        controller: 'LoginCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });

    $locationProvider.html5Mode(true);
  });

And ofcourse, main.js looks like this:

'use strict';

angular.module('orderyourselfApp')

.controller('LoginCtrl', function ($scope, $http) {
    console.log('saysomething');
    $scope.hello = 'Hello world! LoginCtrl';
})

.controller('MainCtrl', function ($scope, $http) {
    console.log('shit is getting real');
    $http.get('/api/awesomeThings').success(function(awesomeThings) {
        $scope.awesomeThings = awesomeThings;
    });
});

Now, the issue is weird and simple: when I do a localhost:9000/login, I should get to the login.html template and LoginCtrl, but no. I keep ending up at index and MainCtrl and nothing seems to change the result.

login.html

  <body ng-app="orderyourselfApp">
    <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

    <!-- Add your site or application content here -->
    <div class="container" ng-controller="LoginCtrl">{{hello}}</div> … then the usual stuff

Where's this going wrong?

Imperfective answered 2/4, 2014 at 12:13 Comment(1)
you have to use ngView directive somewhere in index.html, and login template will be inserted there. So it doesn't need ng-app anymoreFortalice
E
12

First off, templateUrl should point to your actual partial's relative path, for example: templateUrl: 'partials/login.html'.

Then, I think you should first land on http://localhost:9000/ and then follow a link to http://localhost:9000/login

Anyway, you partials should not contain the ng-app declaration (they actually are partial). So you should have an index.html that would define your page's template, and two partials: main.html and login.html, neither of which should contain the ng-app declaration.

Ending in something like this:

  .config(function ($routeProvider, $locationProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'partials/main.html',
        controller: 'MainCtrl'
      })
      .when('/login', {
        templateUrl: 'partials/login.html',
        controller: 'LoginCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });

    $locationProvider.html5Mode(true);
  });

With index.html being the page's landing from http://localhost:9000/

Estimate answered 2/4, 2014 at 12:36 Comment(3)
Also there has to be <div ng-view></div> in index.htmlTopper
@blint you're partially right, what works is adding the ng-view to index, but adding the full path in the templateUrl is not needed.Imperfective
Another +1 to @Topper for ng-view. Saved my night!Housewarming

© 2022 - 2024 — McMap. All rights reserved.