Oleksandr Verhun
Oleksandr Verhun

Reputation: 854

Angular routing

recently I've done angular tutorial at angular getting started. Then I tried to do a little example by myself, but the routing doesn't seem to work. When I start the page the only thing I see is white.Just to mention the path I'm looking is http://localhost:8000/app/index.html#/title. My app looks like this:


index.html

<!doctype html>
<html lang="en" ng-app="test">
<head>
  <meta charset="utf-8">
  <title>Test</title>
  <link rel="stylesheet" href="css/app.css">
  <link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">

  <script src="bower_components/jquery/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>
  <script src="bower_components/angular-mocks/angular-mocks.js"></script>
  <script src="bower_components/angular-animate/angular-animate.js"></script>

  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
</head>

<body>
  <div ng-view></div>
</body>

</html>

app.js

var test = angular.module( 'test', [ 'ngRoute', 'testControllers' ] );

test.config( ['$routeProvider',
    function($routeProvider) {
      $routeProvider.
          when('/title', {
            templateUrl: 'title.html',
            controller: 'TitleCtrl'
          }).
          otherwise({
            redirectTo: '/title'
          });
    }
]);

controllers.js

var testControllers = angular.module( 'testControllers', [] );

testControllers.controller( 'TitleCtrl', [ '$scope',
    function($scope) {
      $scope.select_system = [
        "MOAB",
        "Amoeba",
        "Component Library",
        "SLURM",
        "UniCloud",
        "UniCluster",
        "UNICORE",
        "ProActive"
      ];
    }
]);

templates/title.html

<select ng-repeat="system in select_system">
  <option>{{system}}</option>
</select>

My path looks like this: TextExample - app - app/bower_components - app/js/app.js - app/js/controllers.js - templates/title.html


bower.json
{
  "name": "TestExample",
  "description": "TestProject",
  "version": "0.0.0",
  "authors": [
    "Vyivrain <[email protected]>"
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "private": true,
  "dependencies": {
    "angular": "1.3.x",
    "angular-mocks": "1.3.x",
    "jquery": "1.10.2",
    "bootstrap": "~3.1.1",
    "angular-route": "1.3.x",
    "angular-resource": "1.3.x",
    "angular-animate": "1.3.x"
  }
}

.bowerrc

{
  "directory": "app/bower_components",
  "interactive": false
}

package.json

{
  "name": "TestExample",
  "version": "0.0.0",
  "description": "TestProject",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "postinstall": "bower install",
    "prestart": "npm install",
    "start": "http-server -a 0.0.0.0 -p 8000",
    "test": "echo \"Error: no test specified\" && exit 1",
    "preupdate-webdriver": "npm install"
  },
  "author": "Vyivrain",
  "license": "BSD-2-Clause",
  "dependencies": {
    "bower": "~1.3.12",
    "http-server": "~0.6.1",
    "karma": "~0.12.24",
    "karma-chrome-launcher": "~0.1.5",
    "karma-jasmine": "~0.1.5",
    "protractor": "~1.0.0",
    "shelljs": "~0.2.6",
    "tmp": "~0.0.23"
  }
}

Upvotes: 2

Views: 784

Answers (5)

The reason for not showing your title.html is because it is not getting the right path of title.html file , maybe you have put title.html in some folder, usually we put all application view in templates folder or views folder , so you have change the route according. For example if you put your views in views folder so you have to change route templateUrl in app.js like this.

var test = angular.module( 'test', [ 'ngRoute', 'testControllers' ] );

test.config( ['$routeProvider',
  function($routeProvider) {
    $routeProvider.
       when('/title', {
         templateUrl: 'views/title.html',
         controller: 'TitleCtrl'
      }).
      otherwise({
        redirectTo: '/title'
      });
  }
]);

Upvotes: 0

Kentonbmax
Kentonbmax

Reputation: 958

If you can upgrade; please upgrade to Angular 1.5 and use the New-Router which makes good use of components. By doing this you will be more prepared for Angular 2.0 upgrade when its released.

Upvotes: 0

Seems that in your route.js you missed to write templateUrl : 'templates/title.html'. Instead you have written templateUrl : 'title.html'.

Other solution could -

In your index.html write this in bottom:

<script type="text/ng-template" id="title.html">
 <select ng-repeat="system in select_system">
  <option>{{system}}</option>
 </select>
</script>

In this way you don't have to make a new file called title.html. Very useful if your template is small and frequently used.Also as there is no ajax call to get the template so it is rendered faster.

But more than this, always look your console in browser. In case of any error, angular js provides very good logs to detect it. As in your case you must be receiving an error.

Upvotes: 1

Yashdeep Hinge
Yashdeep Hinge

Reputation: 405

Check chrome dev tools network tab you may see

cannot send xhr in data:// or file:// mode only available in https:// http:// mode

now the solution either make your browser to serve xhr in all modes or make a node server or anyother that you like

Upvotes: 0

jet miller
jet miller

Reputation: 275

app.js must look like this:

            var test = angular.module( 'test', [ 'ngRoute', 'testControllers' ] );

            test.config( ['$routeProvider',
                function($routeProvider) {
                  $routeProvider.
                      when('/title', {
                        templateUrl: 'templates/title.html',
                        controller: 'TitleCtrl'
                      }).
                      otherwise({
                        redirectTo: '/title'
                      });
                }
            ]);

Upvotes: 4

Related Questions